【问题标题】:I have been trying to retrieve data from Firestore to Future builder of Flutter but on first build I get error message I get data after refreshing it我一直在尝试将数据从 Firestore 检索到 Flutter 的 Future builder 但在第一次构建时我收到错误消息我在刷新后得到数据
【发布时间】:2020-07-02 15:19:29
【问题描述】:

enter image description here[在此处输入图像描述][2] Future _getName() async{ 返回等待 Firestore.instance.collection("admin_details").document(_username).get(); }

FutureBuilder<DocumentSnapshot>(
                  future: _getName(),
                  builder: (context,snapshot) {
                    if (snapshot.connectionState == ConnectionState.done) {
                      if (snapshot.hasData) {
                        if (snapshot.data != null) {
                          return Text(snapshot.data['first_name']);
                        }
                        else {
                          return Text("Loading...");
                        }
                      }

                      else {
                        return Text("No Data");
                      }
                    }
                    else{
                      return null;
                    }
                  }
              ),

════════小部件库捕获的异常═════════════════════════════════════ ═════════════════════ 在构建 FutureBuilder(dirty, state: _FutureBuilderState#8f31b) 时引发了以下 NoSuchMethodError: 在 null 上调用了方法“[]”。 接收方:空 尝试调用:

相关的导致错误的小部件是: FutureBuilder 文件:///D:/Flutter/ecoleami1_0/lib/StudentActivity.dart:52:28 抛出异常时,这是堆栈:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)

1 DocumentSnapshot.[](包:cloud_firestore/src/document_snapshot.dart:31:42)

2 _StudentActivityPageState.build。 (包:ecoleami1_0/StudentActivity.dart:57:50)

3 _FutureBuilderState.build(包:flutter/src/widgets/async.dart)

4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)

... ══════════════════════════════════════════════════ ══════════════════════════════════════════════════

有人可以帮帮我吗?

【问题讨论】:

  • 我在热加载应用程序后获取数据
  • 将您的 Firestore 添加到问题中
  • 添加了我的 Firestore 结构的图像

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

我现在正在打电话,所以不能 100% 确定,但这可能是,因为当连接状态不是这样做时,您的代码会返回 null:

将'return null'行替换为return Text('loading');

【讨论】:

  • if (snapshot.connectionState == ConnectionState.done) { if (snapshot.hasData) { if (snapshot.data != null) { return Text(snapshot.data['first_name']); } else { 返回文本(“正在加载...”); } } else { 返回文本(“无数据”); } } else{ 返回文本(“正在加载”);我试过你说的,但问题仍然相同,得到同样的错误,我在热重载后得到数据。
  • 你重建了应用程序吗?
【解决方案2】:

我猜这个错误是由于应用程序中的状态管理不善造成的..

【讨论】:

    【解决方案3】:

    试试这个

    StreamBuilder(
                stream: Firestore.instance
                    .collection("admin_details")
                    .document('name of the document')
                    .snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    var username = snapshot.data;
                    return Text(username['first_name']);
                  }
                },
              ),
    

    【讨论】:

    • 当他需要 futurebuilder 时,为什么要让他使用 streambuilder?
    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-02-16
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2018-02-01
    相关资源
    最近更新 更多