简体   繁体   English

在 Flutter 的嵌套文档内的嵌套数组内从 Firebase 实时数据库获取结果流

[英]Get stream of results from Firebase Realtime Database inside nested array inside nested document in Flutter

I am currently developing a chatting page on my mobile application and was using Realtime Database to reduce costs.我目前正在我的移动应用程序上开发一个聊天页面,并使用实时数据库来降低成本。 Not having much Realtime Database experience, I am running into issues developing a page for active chatrooms where each user has a document with their uid where there is an array of chats .没有太多的实时数据库经验,我在为活动聊天室开发页面时遇到了问题,其中每个user都有一个带有他们的uid的文档,其中有一系列chats How can I generate a list of the first names based on the items inside of the array of chats .如何根据chats数组中的项目生成名字列表。

在此处输入图像描述

I realize it's best practice to show code you have tried, however, I have only used Firebase Firestore and have no idea where to start, considering the complexity of the structure我意识到展示您尝试过的代码是最佳做法,但是,考虑到结构的复杂性,我只使用过 Firebase Firestore 并且不知道从哪里开始

You can try this answer.你可以试试这个答案。 Here I have created StreamBuilder for getting data once.这里我创建了一次获取数据的 StreamBuilder。 As I have provided, Stream outside of StreamBuilder means it will create a stream only once.正如我所提供的,StreamBuilder 之外的 Stream 意味着它只会创建一次流。

final _snap = FirebaseDatabase.instance
      .ref()
      .child(`users/${uid}`)

StreamBuilder<Event>(
  stream: _snap.onValue,
  builder: (BuildContext context, snapshot) {
    if (snapshot.hasData) {
      DataSnapshot snap = snapshot.data as DataSnapshot;
      List<dynamic> chatArray = snap.value['chats'];
      List<String> firstNames = [];
      for (var chat in chatArray) {
        String firstName = chat[recepientFirstName];
        firstNames.add(firstName);
      }
      return ListView.builder(
        itemCount: firstNames.length,
        itemBuilder: (context, index) {
          return Text(firstNames[index]);
        },
      );
    } else {
      return Text('Loading...');
    }
  },
)

Found similar reference about this Questions so you can also visit these threads:找到了关于此问题的类似参考,因此您也可以访问这些线程:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM