【问题标题】:Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist, Firebase Flutter错误状态:无法在不存在的 DocumentSnapshotPlatform 上获取字段,Firebase Flutter
【发布时间】:2021-05-19 18:11:18
【问题描述】:

我正在按文档 ID 获取数据,但出现此错误:

错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段

工作,我可以通过文档ID从firebase获取数据,但它在调试控制台中给出了错误。

我正在使用 StreamBuilder 获取数据:

 StreamBuilder(
          stream: _databaseService.productCollection.doc(docID).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(
                  valueColor:
                      new AlwaysStoppedAnimation<Color>(Colorsx.mainColor),
                ),
              );
            }
            var document1 = snapshot.data;

            return Container(
              decoration: BoxDecoration(
                color: Colorsx.mainColor,
                borderRadius: radius,
              ),
              // color: Colorsx.mainColor,
              child: Column(
                children: [
                  Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Chip(
                          label: LabelText1("Ürün Adı:  "),
                          backgroundColor: Colorsx.mainColor,
                        ),
                        Chip(
                          shadowColor: Colorsx.mainColor2,
                          elevation: 24,
                          label: LabelText1(document1["productName"] ?? ""),
                          backgroundColor: Colorsx.mainColor2,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            );
          },
        ),

我在这里找不到问题,但根据我的研究,它与 ma​​p 相关。 有什么想法吗?

【问题讨论】:

    标签: android firebase flutter dart google-cloud-firestore


    【解决方案1】:

    看起来 _databaseService.productCollection.doc(docID) 在运行此代码时可能不会指向现有文档。如果您随后在其上调用document1["productName"],则会引发您看到的错误。

    所以你需要决定当这种情况发生时要渲染什么(即使只是短暂的)。例如,您可以让CircularProgressIndicator 留在屏幕上,直到文档可用:

    if (!snapshot.hasData || !snapshot.data.exists) {
    

    【讨论】:

      【解决方案2】:

      如果您使用 cloud_functions ^2 并且使用 1,您可能会遇到这个问题,处理错误的唯一方法是使用 try catch 函数

      getSnapshot(DocumentSnapshot snapshot, String key) {
       try {
        return snapshot.get(key);
        } catch (error) {
         return null;
       }
      }
      

      那就叫它吧

      Status.fromSnapShot(DocumentSnapshot snapshot)
        : assert(snapshot.exists != false),
          name = getSnapshot(snapshot, 'name'),
      

      原来是这样的

      Status.fromSnapShot(DocumentSnapshot snapshot)
        : assert(snapshot.exists != false),
          name = snapshot.data()['name'],
      

      【讨论】:

        猜你喜欢
        • 2021-10-26
        • 2021-05-16
        • 2021-06-17
        • 2022-09-27
        • 1970-01-01
        • 2021-04-27
        • 2021-08-15
        • 1970-01-01
        • 2021-05-16
        相关资源
        最近更新 更多