flutter - Listview 构建器区域上的“NoSuchMethodError:getter 'length' 被调用为 null”
问题描述
我想问一下我遇到的错误。所以在有人声称这个问题与其他人相似之前,也许是的,但其他人的回答仍然无法解决我的问题。
所以,我的代码有问题,错误显示大约 5 秒,然后显示数据列表。然后我搜索解决方案,我找到了 futurebuilder 的答案,但是当我调试时,错误仍然显示并且它是永久性的,而不是像以前那样更改为数据列表。这是我的代码
List data;
Future<String> loadJsonData() async {
var jsonText = await rootBundle.loadString('Json/indomodel.json');
if(this.mounted){
setState(() {
data = json.decode(jsonText);
});
}
//print(jsonText);
}
@override
void initState() {
super.initState();
this.loadJsonData();
//loaddata();
}
@override
Widget build(BuildContext context) {
//var buildContext = (BuildContext context, AsyncSnapshot snapshot);
return Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
body: Container(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
height: 60.0,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Colors.white),
child: TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
),
alignLabelWithHint: true,
focusColor: Colors.white,
hintText: "Ketik Kata",
hintStyle: TextStyle(color: Colors.grey),
//labelText: "Bahasa Indonesia",
icon: Icon(
FontAwesomeIcons.search,
color: Colors.grey,
size: 15.0,
),
labelStyle: TextStyle(color: Colors.grey)),
cursorColor: Colors.yellow,
style: TextStyle(color: Colors.grey.shade800),
),
),
Container(
padding: EdgeInsets.only(top: 10.0),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.6,
child: FutureBuilder(
future: loadJsonData(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(!snapshot.hasData)return Center(child: CircularProgressIndicator(),);
if(snapshot.connectionState == ConnectionState.done){
if(snapshot.hasError) return Text('Error ${snapshot.error}');
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index){
return Card(
elevation: 0.3,
child: ListTile(
// leading: CircleAvatar(child: Icon(FontAwesomeIcons.adjust),),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
SelectableText(
data[index]['word'],
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.deepPurple.shade800),
),
Text(
"My Kamus",
style: TextStyle(
color: Colors.grey, fontSize: 10),
)
],
),
SizedBox(height: 10.0,),
Text(data[index]['trans'], style: TextStyle(color: Colors.grey.shade700, fontSize: 13.0),)
],
),
),
);
},
);
}
},
)
)
],
),
),
);
解决方案
尝试将您的替换itemCount: snapshot.data.length,
为itemCount: snapshot.data == null ? 0 : snapshot.data.length,
. 那应该解决它。
推荐阅读
- javascript - 准确的javascript睡眠功能
- python - 如何在 python django 中使用 * 替换电子邮件地址的某些字符
- java - authentication.getName() 在自定义 AuthenticationProvider 中返回 null
- sql-server - 从 VB.NET 备份 SQL Server 未生成备份文件,文件未显示在驱动器中
- c# - 凝视定时器完成后如何改变场景?
- docker - Docker端点其他撰写文件
- xslt - 在 Aspose.PDF XSLT 中定义表格边框的语法是什么
- c# - Xamarin TemplatedBinding 不适用于附加属性
- google-apps-script - 从一个电子表格复制/传输到另一个基于代码的
- sql - 使用日期范围和分组数据加入两个表