I want to get length of group field and set it to ListView.builder item count property,
There is image of my firebase collections
my code here
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("users").snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
final isCollectionEmpty = snapshot.data!.docs.isEmpty;
final DocumentsWhichContainsPosts = snapshot.data!.docs.where(
(doc) =>
(doc.data() as Map<String, dynamic>)["groups"].isNotEmpty);
if (DocumentsWhichContainsPosts.isNotEmpty) {
return ListView.builder(
itemCount: // get length of group field in user collection & document
itemBuilder: ((context, index) {
return Text("data");
}),
);
} else {
return Container(
child: Center(child: Text("No posts")),
);
}
} else {
return const Center(
child: CircularProgressIndicator(color: Colors.red),
);
}
});
2
Answers
Did it with this code,
You will probably need to use a nested ListView.builder, because you want to get the data of a particular user first, which you can do in a parent ListView. Once you get the current user data, you can use its "groups" property in a child ListView.
Here’s an example: