【发布时间】:2020-11-28 16:56:32
【问题描述】:
我想在 ListView.builder 为空时打印空(当我在 Firebase 中没有数据时)并且我正在使用 FutureBuilder。
另外,我正在从 Firebase 检索这些数据。当我在 firebase “NoSuchMethodeError: The method 'forEach' was called on null”中没有数据时,我想这样做以避免此错误。
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,
);
},
);
【问题讨论】:
-
不想将此作为答案发布。您可以在返回 ListView 之前添加一个条件来检查“列表”是否为空。 " if(lists.isEmpty) return Container(); "
标签: flutter listview dart widget runtime-error