【问题标题】:Flutter & Firebase : Failed to get data from firebase sub-collection into my StreamFlutter & Firebase:无法从 firebase 子集合中获取数据到我的 Stream
【发布时间】:2021-03-06 04:37:26
【问题描述】:

我正在尝试获取我的数据信息流。由于某种原因,我做不到。它甚至没有打印出我的内部打印语句“print(“Inside snapshot”)”。

请查看我的代码并告诉我如何解决此问题。 我期待听到您的所有意见和想法。在此先感谢您。

终端消息

W/DynamiteModule( 3822): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 3822): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 3822): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/flutter ( 3822): Inside _messageList

型号

class Conversation {
  final DateTime date;
  final String message;
  final String sendby;

  Conversation({this.date, this.message, this.sendby});
}

屏幕代码

return StreamBuilder<List<Conversation>>(
        stream: User_DatabaseService(uid: uid).chatroomMesssages,
        builder: (context, snapshot) {
          List<Conversation> messageList = snapshot.data ?? [];
          messageList.forEach((message) {
        print("${message.date} ${message.message} ${message.sendby}");
      });
//-----
}

数据库代码

    // collection reference
  final CollectionReference chatroomCollection =
      FirebaseFirestore.instance.collection('chatrooms');
// all conversation
      List<Conversation> _messageList(QuerySnapshot snapshot) {
        print("Inside _messageList");
    
        return snapshot.docs.map((doc) {
          print("Inside snapshot");
          final dataInfo = doc.data();
          print(dataInfo['date']);
          
          return Conversation(
            date: dataInfo['date'] ?? null,
            sendby: dataInfo['sendBy'] ?? null,
            message: dataInfo['message'] ?? null,
          );
        }).toList();
      }
    
      //Get all conversation
      Stream<List<Conversation>> get chatroomMesssages {
        //print('in allUserData');
        print("Inside getChatroomMesssages");
        return userCollection
            .doc('devil@yahoo.com_punreach@yahoo.com')
            .collection("chats")
            .snapshots()
            .map(_messageList);
      }

火力基地

【问题讨论】:

  • 是否有任何错误,或任何其他相关输出? userCollection 是什么?
  • 它应该给我子集合数据。但它没有
  • 哦,是的,我用错了名字。
  • 写下你的答案!我会给你我的积分
  • 我刚刚做到了!很高兴能帮上忙。

标签: flutter google-cloud-firestore stream state-management


【解决方案1】:

Firebase 中没有“此集合不存在”错误。您可以参考不存在的集合,它们到达时是空的。您可以引用不存在的集合,这就是您首先创建它们的方式。

您的错误是您使用的是userCollection,它没有任何这些。在您的屏幕截图中,我们看到了chatrooms,因此您应该改用chatroomsCollection,它实际上包含我们在屏幕截图中看到的数据。

【讨论】:

  • 我会在22小时后给你
  • 是时候了吗? :D.
【解决方案2】:

请阅读错误信息,它实际上提供了很多信息:

类型“对话”不是类型“列表”的子类型

看看这一行;如果snapshot.data 为空,那么您正在尝试分配错误类型的对象:

List<Conversation> messageList = snapshot.data ?? Conversation();

编辑:再次,请阅读实际的错误消息。

在 null 上调用了方法“forEach”。

现在,如果snapshot.data 为空,则将null 分配给同一个变量,此处为:List&lt;Conversation&gt; messageList = snapshot.data ?? null;,然后您将调用.forEach

【讨论】:

  • 哦,这只是我之前的代码。我改变了它。请看一下新的。
  • 是的,我明白了。我分配了null。但这不是重点。 Cus 如果它有一些价值,它会从 null 变成一些东西。但我没有
  • 是的,这的重点。因为它是空的,所以抛出异常。您应该为其分配一个空列表,或者根据您的设计要求以不同的方式处理此类情况。我不确定如何进一步解释。
  • 先生,我按你说的改了。而现在,我仍然无法将价值融入我的信息流中。那你怎么解释呢?
猜你喜欢
  • 2021-03-05
  • 2020-09-07
  • 2022-09-23
  • 2020-06-07
  • 2021-07-23
  • 2021-07-25
  • 2021-05-07
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多