【问题标题】:Flutter-Firebase read from firestore multiple fields including arrayFlutter-Firebase 从 firestore 读取多个字段,包括数组
【发布时间】:2021-11-14 10:33:42
【问题描述】:

我正在尝试从 firestore 读取我的 uid1 和 uid2 文档中的所有字段。我的代码崩溃了,因为 fromJson 无法将 imageList 从数组转换为 List。

Future<List<AppClient>> getClientListResult(List<String> listId) async{
    final List<AppClient> clientList = <AppClient>[];
    print(listId);
    final QuerySnapshot<Map<String, dynamic>> snapshot = await _firestore.collection('clients').where('uid', arrayContainsAny: listId).get();
    final List<AppClient> result = snapshot.docs.map((QueryDocumentSnapshot<Map<String, dynamic>> doc) => AppClient.fromJson(doc.data())).toList();
    clientList.addAll(result);
    return clientList;
  }

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    你可以使用 rxdart 来组合来自 firestore 的两个集合使用这个插件rxdart

    sample code
    StreamBuilder(
                                      stream: CombineLatestStream.list([
                                        firestore
                                            .collection('users')
                                            .doc(data['id'])
                                            .snapshots(),
                                        firestore
                                            .collection('Image')
                                            .doc(data['id'])
                                            .collection('Photos')
                                            .orderBy('timestap')
                                            .snapshots(),
                                      ]),
                                      builder: (context, snapshotid) {
    // here for the 1st stream
                                        final active = snapshotid.data[0].data();
    // here is the for the second stream
                                        final active1 =
                                            snapshotid.data[1].docs[0].data();
    

    【讨论】:

      【解决方案2】:

      您的 AppClient 类应该类似于以下内容:

      class AppClient {
        String address;
        String email;
        List<String> imageList;
        String name;
        String price;
        double rating;
        List<String> uid;
      
        AppClient(
            {this.address,
            this.email,
            this.imageList,
            this.name,
            this.price,
            this.rating,
            this.uid});
      
        AppClient.fromJson(Map<String, dynamic> json) {
          address = json['address'];
          email = json['email'];
          imageList = json['imageList'].cast<String>();
          name = json['name'];
          price = json['price'];
          rating = json['rating'];
          uid = json['uid'].cast<String>();
        }
      
        Map<String, dynamic> toJson() {
          final Map<String, dynamic> data = new Map<String, dynamic>();
          data['address'] = this.address;
          data['email'] = this.email;
          data['imageList'] = this.imageList;
          data['name'] = this.name;
          data['price'] = this.price;
          data['rating'] = this.rating;
          data['uid'] = this.uid;
          return data;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-28
        • 1970-01-01
        • 2022-12-08
        • 2021-03-05
        • 1970-01-01
        • 2020-05-05
        • 2022-09-23
        • 2018-11-26
        相关资源
        最近更新 更多