【问题标题】:Dart/Flutter - the method forEach was called on nullDart/Flutter - 在 null 上调用了 forEach 方法
【发布时间】:2021-10-19 12:25:26
【问题描述】:

我正在尝试从 firebase rtdb 获取“Child_Name”和“Parent_Name”,并使用 ListView.builder 创建名称列表。我之前在应用程序的另一部分已经这样做了,它运行良好。我正在尝试再次应用相同的逻辑,但出现错误。

错误发生在 setStatechildrenList = Map.from(value) 所在的位置。

我的 firebase rtdb 视图 is here (image)

错误:

- [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'forEach' was called on null.
- Tried calling: forEach(Closure: (dynamic, dynamic) => void)

代码(1):

    Future<List> getListOfChildren() async {
      print("Getting Children");
      databaseReference
      .child("users")
      .child("Absent_Children")
      .child(formattedDate)
      .onValue
      .listen(
        (event) {
          setState(
            () {
              var value = event.snapshot.value;
              childrenList = Map.from(value)
                  .values
                  .map((e) => Children.fromJson(Map.from(e)))
                  .toList();
          },
        );
      },
    );
    return childrenList;
  }

代码(2):数据类

class Children {
  final String childName;
  final String parentName;

  Children({
    this.childName,
    this.parentName,
  });

  static Children fromJson(Map<dynamic, dynamic> json) {
    return Children(
      childName: json["Child_Name"],
      parentName: json["Parent_Name"],
    );
  }
}

代码(4):格式化日期

  getTodaysDate() {
    setState(
      () {
        DateTime now = DateTime.now();
        var date = DateFormat("dd-mm-yyyy");
        formattedDate = date.format(now).toString();
      },
    );
  }

代码(3):我的 ListView.builder

body: childrenList.isEmpty
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: childrenList.length,
              itemBuilder: (context, int index) {
                final Children child = childrenList[index];
                final String childName = child.childName;
                final String parentName = child.parentName;
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Card(
                    elevation: 0,
                    child: ExpansionTile(
                      title: Text(
                        childName.toUpperCase(),
                        style: GoogleFonts.lexendMega(),
                        textAlign: TextAlign.center,
                      ),
                      children: [
                        Column(
                          children: [
                            Text(
                              parentName,
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lexendMega(fontSize: 13),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                );
              },
            ),

谢谢。

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database


    【解决方案1】:

    正如我所说的here 一样,看起来databaseReference.child("users").child("Absent_Children").child(formattedDate) 没有数据,并且您的代码无法处理这种情况。

    如果没有数据是正常的,你应该在尝试访问它的值之前检查快照是否有值:

      databaseReference
      .child("users")
      .child("Absent_Children")
      .child(formattedDate)
      .onValue
      .listen(
        (event && event.snapshot.exists) { // ? add exists check here
          setState(
            () {
              var value = event.snapshot.value;
              childrenList = Map.from(value)
                  .values
                  .map((e) => Children.fromJson(Map.from(e)))
                  .toList();
          },
        );
    

    【讨论】:

    • 我明白了。由于数据库中有数据,因此获取数据的正确方法是什么?我使用类似的方式从同一个数据库但从不同的节点获取数据,数据显示没有问题。
    • 我已经解决了这个问题。问题在于我如何获得 formattedDate (没有时间)。这就是没有获取数据的原因。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 2020-01-22
    • 2020-11-06
    • 2019-09-23
    • 2020-06-04
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多