I'm trying to retrieve posts from a Firestore collection called "posts", which contains the post creator's userID and post description and this is possible by using both StreamBuilder and FutureBuilder(Not preferable, because it gets a snapshot only once and doesn't update when a field changes).
However, I want to query another collection called "users" with the post creator's userID and retrieve the document that matches the userId.
This was my first approach:
StreamBuilder<QuerySnapshot>(
stream:Firestore.instance.collection("posts").snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: _showProgressBar,
);
}
List<DocumentSnapshot> reversedDocuments = snapshot.data.documents.reversed.toList();
return ListView.builder(
itemCount: reversedDocuments.length,
itemBuilder: (BuildContext context, int index){
String postAuthorID = reversedDocuments[index].data["postAuthorID"].toString();
String postAuthorName = '';
Firestore.instance.collection("users")
.where("userID", isEqualTo: postAuthorID).snapshots().listen((dataSnapshot) {
print(postAuthorName = dataSnapshot.documents[0].data["username"]);
setState(() {
postAuthorName = dataSnapshot.documents[0].data["username"];
});
});
String desc = reversedDocuments[index].data["post_desc"].toString();
return new ListTile(
title: Container(
child: Row(
children: <Widget>[
Expanded(
child: Card(
child: new Column(
children: <Widget>[
ListTile(
title: Text(postAuthorName, //Here, the value is not changed, it holds empty space.
style: TextStyle(
fontSize: 20.0,
),
),
subtitle: Text(desc),
),
)
After understanding that ListView.builder() can only render items based on the DocumentSnapshot list and can't handle queries inside the builder.
After many research: I tried many alternatives like, trying to build the list in the initState(), tried using the Nested Stream Builder:
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('posts').snapshots(),
builder: (context, snapshot1){
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("users").snapshots(),
builder: (context, snapshot2){
return ListView.builder(
itemCount: snapshot1.data.documents.length,
itemBuilder: (context, index){
String desc = snapshot1.data.documents[index].data['post_description'].toString();
String taskAuthorID = snapshot1.data.documents[index].data['post_authorID'].toString();
var usersMap = snapshot2.data.documents.asMap();
String authorName;
username.forEach((len, snap){
print("Position: $len, Data: ${snap.data["username"]}");
if(snap.documentID == post_AuthorID){
authorName = snap.data["username"].toString();
}
});
return ListTile(
title: Text(desc),
subtitle: Text(authorName), //Crashes here...
);
},
);
}
);
}
);
Tried with Stream Group and couldn't figure out a way to get this done, since it just combines two streams, but I want the second stream to be fetched by a value from first stream.
This is my Firebase Collection screenshot:
I know this is a very simple thing, but still couldn't find any tutorial or articles to achieve this.
setState
, this is in a build method. In general though, you should stay away from querying Firestore in a build method. Check out this excellent answer on keeping your build method pure. – UnspeakableinitState
method? – Unspeakable