我想问一下我遇到的错误。所以在有人声称这个问题与别人相似之前,也许是的,但是其他人的回答仍然无法解决我的问题。
所以,我的代码有问题,错误显示大约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),)
],
),
),
);
},
);
}
},
)
)
],
),
),
);
答案 0 :(得分:1)
尝试将itemCount: snapshot.data.length,
替换为itemCount: snapshot.data == null ? 0 : snapshot.data.length,
。那应该解决。
答案 1 :(得分:0)
由于没有数据,snapshot.data
返回null
并且您正在ListView
内部使用它,因此您所要做的就是使用这种方法:
FutureBuilder(
future: loadJsonData(),
builder: (BuildContext context, AsyncSnapshot snapshot){
// if there is no data yet, show progress indicator.
if (!snapshot.hasData) return Center(child: CircularProgressIndicator());
// only show ListView if you have data
return ListView.builder(...);
}
)
我是从手机上键入此代码的,因此可能存在一些基本的语法错误。