Flutter:在 null 上调用了方法“[]”
Posted
技术标签:
【中文标题】Flutter:在 null 上调用了方法“[]”【英文标题】:Flutter: The method '[ ]' was called on null 【发布时间】:2020-11-06 18:52:02 【问题描述】:我收到以下代码的以下错误。 https://github.com/Avi-nash5/CP-flutter-app/blob/master/lib/PlatformScreen.dart 错误屏幕显示 1 秒,然后按预期加载。请帮忙。 error how it is supposed to be
【问题讨论】:
您能否发布导致错误的相关代码而不是整个文件,因为如果不运行代码就更难调试。错误可能与data[index]['name']
相关,因为您在 Text
小部件中调用它,并且它可能在开始时为空,因此您需要执行空检查。
【参考方案1】:
您可以复制粘贴PlatformScreen
的完整代码data
在使用时未准备好
你可以使用 bool _isLoading
来控制,如果data
没有准备好显示CircularProgressIndicator
bool _isLoading = true;
...
Future<String> getJsonData() async
...
setState(()
_isLoading = false;
);
...
child: _isLoading
? Center(child: CircularProgressIndicator(backgroundColor: Colors.white,))
: ListView.builder(
工作演示
PlatformScreen
的完整代码
import 'dart:convert';
import 'PlatformModel.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:http/http.dart' as http;
class PlatformScreen extends StatefulWidget
final Platform platform;
PlatformScreen(this.platform);
@override
PlatformScreenState createState() => PlatformScreenState();
class PlatformScreenState extends State<PlatformScreen>
List data;
int n;
bool _isLoading = true;
@override
void initState()
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_)
getJsonData();
);
Future<String> getJsonData() async
var response = await http.get(Uri.encodeFull(widget.platform.url),
headers: "Accept": "application/json");
print(response.body);
data = jsonDecode(response.body);
n = data.length;
print(data);
convert();
setState(()
_isLoading = false;
);
return "Success";
void convert()
print(n);
var now = new DateTime.now();
now = now.toLocal();
for (int i = 0; i < n; i++)
var start = DateTime.parse(data[i]['start_time']);
var end = DateTime.parse(data[i]['end_time']);
start = start.toLocal();
end = end.toLocal();
data[i]['start_time'] = start.toString();
data[i]['end_time'] = end.toString();
print(data);
@override
Widget build(BuildContext context)
return Scaffold(
backgroundColor: Theme.of(context).accentColor,
body: Column(
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
top: 25.0,
),
child: Container(
margin: EdgeInsets.all(10.0),
height: 125,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(30.0),
),
child: Stack(
children: <Widget>[
Hero(
tag: widget.platform.imgurl,
child: ClipRRect(
borderRadius: BorderRadius.circular(30.0),
child: Image(
height: 125,
width: 125,
image: AssetImage(widget.platform.imgurl),
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(125, 0, 0, 0),
child: Container(
child: Center(
child: Text(
widget.platform.name,
style: TextStyle(
color: Colors.white,
fontSize: 32,
),
),
),
),
),
],
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
),
child: _isLoading
? Center(child: CircularProgressIndicator(backgroundColor: Colors.white,))
: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: n,
itemBuilder: (BuildContext context, int index)
return Padding(
padding: const EdgeInsets.fromLTRB(
15.0, 15.0, 10.0, 10.0),
child: Container(
height: 150,
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius:
BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: Colors.black54,
offset: Offset(0, 10.0),
blurRadius: 10.0,
)
]),
child: Column(
children: <Widget>[
Center(
child: Text(
data[index]['name'],
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
),
],
),
),
);
),
),
),
],
),
),
),
],
),
);
【讨论】:
以上是关于Flutter:在 null 上调用了方法“[]”的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 错误在 null 上调用了方法“[]”。接收方:null 尝试调用:[]("videoId")
Flutter Firebase 在 null 上调用了方法“数据”
Flutter NoSuchMethodError:在 null 上调用了方法“[]”。使用flutter获取api时如何解决此错误