【问题标题】:Retrieve data with StreamBuilder from collection and subCollection in same time Flutter cloud Firestore在 Flutter 云 Firestore 中同时使用 StreamBuilder 从集合和子集合中检索数据
【发布时间】:2021-03-10 21:53:42
【问题描述】:

我有一个名为UserCollection 的集合, 在这个集合中,我有一个包含其他特定文档的子集合,称为 'UserSubCollection'

使用 StreamBuilder 我试图获取这些集合失败

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('UserCollection')
          .doc(firebaseUser.uid).collection('UserSubCollection')
              .snapshots(),
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError)
              return Center(child: Text("Error"));
            else if (snapshot.data.docs.isEmpty) //also check if empty! show loader?
              return Center(child: Text('Errors'));
            else
            return new ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (BuildContext context, int index) {

我只能得到一个或一个集合或子集合。 有没有办法同时获得它们?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    第一个问题:您的商品是否需要随着时间的推移自动更新?否则,我建议您使用 FutureBuilder 而不是 StreamBuilder。

    要同时获得两者,我建议您使用这样的单独方法:

    Future<List</*INSERT RETURN TYPE*/>> _retrieveUsers() async{
          //get all the users
          QuerySnapshot query = await FirebaseFirestore.instance
                  .collection('UserCollection').get();
          Map<String,Map<String,dynamic>> userMap = Map();
          //add each user to the map
          query.docs.forEach(doc => userMap.putIfAbsent(doc.id, doc.data());
          FutureGroup<QuerySnapshot> queries = FutureGroup(); //add async package in pubspec.yaml [there might be version issues]
          //retrieve sub collections and bind them to the corresponding user
          query.docs.forEach((doc) => queries.add(_retrieveSubCollection(userMap,doc)));
          //Close queries
          queries.close();
          //Await for all the queries to be completed
          await queries.future;
          //now you have for each user a structure containing its data plus everything in its sub collection. Do whatever you want and then return a list or a map according to what you need
          
          return /*YOUR RETURN*/;
     }
     
     Future<Void> _retrieveSubCollection( Map<String,Map<String,dynamic>> userMap, DocumentSnapshot doc) async{
            //retrieve the subcollection of a document using its id
            QuerySnapshot query = query FirebaseFirestore.instance
                  .collection('UserCollection')
              .doc(doc.id).collection('UserSubCollection').get();
            //bind the subcollection to the relative user
            Map<String,dynamic> userData = userMap[doc.id];
            userData.add("subcollection", query.docs.map((subDoc) =>subDoc.data()).toList());
            userMap.update(doc.id,(value)=> userData);
            return;
          }
    

    现在只需使用未来的构建器来显示您的查询结果。

    FutureBuilder(future: _retrieveUsers(), builder: (context,snapshot) {/*YOUR CODE*/}
    

    我实际上并没有在真实环境中尝试过代码,所以如果您在更新用户地图时发现问题,则可能是由于异步更新而导致的问题。但是,由于每个查询都针对地图中的特定元素,我认为不会有问题

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2021-06-28
      • 2020-01-25
      • 1970-01-01
      • 2021-07-03
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      相关资源
      最近更新 更多