0
votes

I am trying to get data from the following path in Flutter from cloud Firestore and I have the following structure

  1. followers/{currentuser}/userFollowers/{user_id}
  2. users/user_id/user_details

I am trying to get details of all the followers for the current user. Can anyone help?

i was able to get the first part by

DocumentSnapshot snapshot =
        await followersRef.document(currentUser.id).get();

I do not know how to loop thru all the id's to fetch their data from users collections. I was trying the following code but no success.

snapshot.data.forEach((key, value) async {
      DocumentSnapshot snapshot2 =
          await usersRef.document(snapshot.documentID).get();
      print('List' + snapshot2.data);
    }
1
can you post a screenshot of your firestore db structure ? - Pooja
have you tried anything? what code did you use? - Chris Papantonis
so i was able to get list of id for followers with 'DocumentSnapshot snapshot = await followersRef.document(currentUser.id).get();' snapshot.data.forEach((key, value) async { DocumentSnapshot snapshot2 = await usersRef.document(snapshot.documentID).get(); print('List' + snapshot2.data.length.toString()); }); - Harshal Gandhi
Can you share a snapshot with a sample of you data on both documents? so we can compare the expected data? - Rafael Lemos

1 Answers

0
votes

You could do something like this:

final db = Firestore.instance;
final uid = firebase.auth().currentUser.uid;

var userDetails = [];
var data = db.collection("followers").doc(uid).collection("userFollowers").get()
 .then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        userDetails.add(db.collection("users").doc(doc.user_id)
          .collection("user_details").get())
    });
});

NOTE: You could simplify this by adding all follower IDs to a followersID array on each current user document, this would increase performance and decrease both number of reads and amount of data managed by your app.