ListView为空时如何打印为空? (扑)
How to print empty when ListView is empty? (Flutter)
我想在 ListView.builder 为空时(当我在 Firebase 中没有数据时)打印空并且我正在使用 FutureBuilder。
此外,我正在从 Firebase 检索此数据。当我在 firebase 中没有数据时,我想这样做以避免出现此错误“NoSuchMethodeError:在 null 上调用了方法 'forEach'”。
return FutureBuilder(
future: retrieve.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
print("object");
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 5),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.purple,
radius: 30,
child: Padding(
padding: EdgeInsets.all(6),
child: FittedBox(
child: Text(
"$" + lists[index]["amount"],
style: TextStyle(
color: Colors.white, fontFamily: 'FjallaOne'),
),
),
),
),
title: Text(
lists[index]["title"] + " " + lists[index]["Picker"],
style: TextStyle(
color: Colors.black, fontFamily: 'FjallaOne'),
),
subtitle: Text(
lists[index]["Date"],
),
),
);
});
}
return SpinKitHourGlass(
color: Colors.purple,
size: 70.0,
);
},
);
我怀疑列表可能为空或为空。因此,只需在 return ListView 之前添加此条件。
if(lists?.isEmpty || lists == null)return Center(child: Text("Empty"));
注意:lists
后的问号确保它不会在列表为空时抛出错误。
您的代码现在应该如下所示:
return FutureBuilder(
future: retrieve.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
print("object");
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
if(lists?.isEmpty || lists == null)return Center(child: Text("Empty"));
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 5),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.purple,
radius: 30,
child: Padding(
padding: EdgeInsets.all(6),
child: FittedBox(
child: Text(
"$" + lists[index]["amount"],
style: TextStyle(
color: Colors.white, fontFamily: 'FjallaOne'),
),
),
),
),
title: Text(
lists[index]["title"] + " " + lists[index]["Picker"],
style: TextStyle(
color: Colors.black, fontFamily: 'FjallaOne'),
),
subtitle: Text(
lists[index]["Date"],
),
),
);
});
}
return SpinKitHourGlass(
color: Colors.purple,
size: 70.0,
);
},
);
另请注意:当列表为空时,ListView.builder()
不会抛出 noSuchMethodError
。抛出错误是因为您return从 Firestore 获取的数据为空。
这有两个可能的原因。您在 Firestore 中查询的字段不存在,或者您使用了错误的语法。
改变这个:
Map<dynamic, dynamic> values = snapshot.data.value;
为此:
Map<dynamic, dynamic> values = snapshot.data['value'];
我想在 ListView.builder 为空时(当我在 Firebase 中没有数据时)打印空并且我正在使用 FutureBuilder。
此外,我正在从 Firebase 检索此数据。当我在 firebase 中没有数据时,我想这样做以避免出现此错误“NoSuchMethodeError:在 null 上调用了方法 'forEach'”。
return FutureBuilder(
future: retrieve.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
print("object");
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 5),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.purple,
radius: 30,
child: Padding(
padding: EdgeInsets.all(6),
child: FittedBox(
child: Text(
"$" + lists[index]["amount"],
style: TextStyle(
color: Colors.white, fontFamily: 'FjallaOne'),
),
),
),
),
title: Text(
lists[index]["title"] + " " + lists[index]["Picker"],
style: TextStyle(
color: Colors.black, fontFamily: 'FjallaOne'),
),
subtitle: Text(
lists[index]["Date"],
),
),
);
});
}
return SpinKitHourGlass(
color: Colors.purple,
size: 70.0,
);
},
);
我怀疑列表可能为空或为空。因此,只需在 return ListView 之前添加此条件。
if(lists?.isEmpty || lists == null)return Center(child: Text("Empty"));
注意:lists
后的问号确保它不会在列表为空时抛出错误。
您的代码现在应该如下所示:
return FutureBuilder(
future: retrieve.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
lists.clear();
print("object");
Map<dynamic, dynamic> values = snapshot.data.value;
values.forEach((key, values) {
lists.add(values);
});
if(lists?.isEmpty || lists == null)return Center(child: Text("Empty"));
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 5),
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.purple,
radius: 30,
child: Padding(
padding: EdgeInsets.all(6),
child: FittedBox(
child: Text(
"$" + lists[index]["amount"],
style: TextStyle(
color: Colors.white, fontFamily: 'FjallaOne'),
),
),
),
),
title: Text(
lists[index]["title"] + " " + lists[index]["Picker"],
style: TextStyle(
color: Colors.black, fontFamily: 'FjallaOne'),
),
subtitle: Text(
lists[index]["Date"],
),
),
);
});
}
return SpinKitHourGlass(
color: Colors.purple,
size: 70.0,
);
},
);
另请注意:当列表为空时,ListView.builder()
不会抛出 noSuchMethodError
。抛出错误是因为您return从 Firestore 获取的数据为空。
这有两个可能的原因。您在 Firestore 中查询的字段不存在,或者您使用了错误的语法。
改变这个:
Map<dynamic, dynamic> values = snapshot.data.value;
为此:
Map<dynamic, dynamic> values = snapshot.data['value'];