【问题标题】:How to print empty when ListView is empty? (Flutter)ListView为空时如何打印空? (扑)
【发布时间】: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


【解决方案1】:

我怀疑列表可能为空或为空。因此,只需在返回 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。由于您从 Firestore 返回的数据为空,因此引发了错误。

这有两个可能的原因。您在 Firestore 中查询的字段不存在,或者您使用了错误的语法。

改变这个:

Map<dynamic, dynamic> values = snapshot.data.value;

到这里:

Map<dynamic, dynamic> values = snapshot.data['value'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多