引用列表上的Flutter Firestore StreamBuilder

时间:2019-01-20 12:18:10

标签: firebase flutter google-cloud-firestore

Firestore数据库由一组用户组成。每个用户文档都有一些公共数据和他遵循的称为“跟随”的用户ID数组。在Flutter应用中,我想在以下数组上创建一个StreamBuilder。因此,当用户将某人添加到他的后续数组中时-该数组将添加到流中,并且当以下列表中的用户更改其数据时-该流也将更新。

我想也许使用引用列表('users/usersid3')而不是ID列表('userid3'),但我不知道如何实现它。

这是数据库的结构方式。

users
      - user1id
            - some public data
            - following: [user2id, user3id]
      - user2id
            - some public data
            - following: []
      - user3id
            - some public data
            - following: [user2id]

1 个答案:

答案 0 :(得分:0)

因此,有一种解决方法可以解决此问题。

  1. 我将以下列表另存为地图(名为“ followingMap”)在单独的文档“ followingDoc”中。
  2. 在该文档上使用了StreamBuilder:
StreamBuilder(
  stream: Firestore.instance.collection('users').document(uid).collection('data').document('followingDoc').snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return Text("No data");
    return ListView.builder(
      itemCount: snapshot.data['followingMap'].keys.toList().length,
      itemBuilder: (BuildContext context, int index) {
        return followingItemWidget(id: snapshot.data['followingMap'].keys.toList()[index]);
      }
    );
  },
)
  1. 最后,我创建了一个名为“ followingItemWidget”的小部件,该小部件在我关注的用户的文档上具有StreamBuilder。

这很完美,尽管我相信应该只有一种StreamBuilder可以做到这一点。