Home > Enterprise >  Flutter-Firebase read from firestore multiple fields including array
Flutter-Firebase read from firestore multiple fields including array

Time:09-21

Im trying to read all the fields inside my uid1 and uid2 document from firestore. My code crashes because fromJson cant convert imageList from array to 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;
  }

enter image description here

CodePudding user response:

you can use rxdart to combine two collection from firestore use this plugin 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();

CodePudding user response:

Your AppClient class should look something similar to this:

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;
  }
}
  • Related