Reputation: 19
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.
Upvotes: 1
Views: 2430
Reputation: 3015
Lets start,
This is your children list List<Widget> children = [];
Change your ListView
children to ListView(children: children, ...)
And update your streambuilder like that,
if(!snapshot.hasData) return new Text("Loading...");
initChildren(snapshot); // here is important
return new ListView(...
And initChildren
is
initChildren(snapshot) async {
final list = await Future.wait(snapshot.data.documents.map((document) async {
String name = '';
String number = '';
var doc = await 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),
);
}));
setState(() => children = list);
}
Upvotes: 1