I have two collections in my firestore db; "queries" and "users". My requirement is to get all the queries with respect to specific flag from "queries" collection. Once done I need to check uid in each query and query my user's data using that uid from "users" collection. In this case I am fetching user's name and number using StreamBuilder in flutter. Below is my approach:
return new StreamBuilder(
stream: db.collection("queries").where("isAttended", isEqualTo: false).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if(!snapshot.hasData) return new Text("Loading...");
return new ListView(
children: snapshot.data.documents.map((document){
String name = '';
String number = '';
var doc = db.collection("users").document(document["uid"]).get();
doc.then((onValue) {
number = onValue.data['number'];
name = onValue.data['name'];
});
return new ListTile(
title: new Text(name),
subtitle: new Text(number),
);
}).toList(),
);
},
);
Problem is onValue returns null. Please advise on my approach for querying data in the manner I specified above. TIA.
then()
callback only runs once the data is loaded, while thereturn new ListTile()
that uses the data from the database runs straight away. I'm not enough of a Flutter expert to show you how to do this, but this answer shows something similar (though not the same). – Frank van Puffelen