首页 > 解决方案 > StreamBuilder 更新颤振后返回错误

问题描述

在更新颤振之后,我在 StreamBuilder 中遇到了一个问题。标有 ** 的行返回错误。我试图通过添加空检查(!)来纠正它,但它返回:

没有为“对象”类型定义运算符“[]”。

我该如何解决?

此外,是否有更优雅的方式(也就是没有 StreamBuilder)来拍摄快照/流?例如,我注意到以下代码行是有效的:

FirebaseFirestore.instance.collection("campdata").doc(docId).snapshots();

但是,我不知道如何进一步开发它,以便它从 firebase 返回可读数据。

非常感谢

  strem_builder_matcheduser(){
      return StreamBuilder(
          stream: firestore.collection("Requests").doc(matched_user_mail).snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final messages = snapshot.data;
              matched_user_connectionstatus = **messages['status of connection']**;
            }
            if(matched_user_connectionstatus=="offline"){
    
              try{
              }catch(e){}
              return SizedBox();
            }
            else
              return Text("");
          }
      );

    }

标签: firebasefluttergoogle-cloud-firestorestream-builder

解决方案


将其更改为:

matched_user_connectionstatus = messages.get('status of connection');

或这个:

final messages = snapshot.data() as Map<String, dynamic>;
matched_user_connectionstatus = messages['status of connection'];

推荐阅读