'How to get list of document from firestore in flutter based on field value
My firestore document contains names and other data too I just want to show names on the ListView but ListView takes itemCount which takes the length of the document but my document contains other values like doc1(name), doc2(name), doc3(name), doc4(other value), doc5(other value), something like that so the length of the document becomes 5 where I want the length of 3.
If I use the where clause it works fine, but then I can't use other data in my second TabBarView.
In other frameworks & languages, we can use the filter method to filter data from observable and display filtered data.
Hope someone understands my situation.
Here is My Code:
final nameList= FirebaseFirestore.instance
.collection('users')
.doc(userId)
.collection('namesList')
//.where('name', isNotEqualTo: '') <------- works fine if I remove comment
.snapshots();
Here is my Streambuilder:
StreamBuilder<QuerySnapshot>(
stream: cropList,
builder:
(BuildContext ctx, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (!snapshot.hasData || snapshot.data!.docs.isEmpty) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return Expanded(
child: TabBarView(
children: [
Expanded(
child: ListView.builder(
itemCount: snapshot.data?.docs.length,
itemBuilder: (ctx, index) {
return Card(
elevation: 3,
child: ListTile(
leading: Text(snapshot.data?.docs[index]
['name']),
),
);
}
),
),
Center(
child: Text('data'),
)
],
),
);
},
),
hence I get an error: Bad state: field does not exist within the DocumentSnapshotPlatform
Solution 1:[1]
It seems like a typo:
I'm assuming the field is name and not Name
Try this:
final nameList= FirebaseFirestore.instance
.collection('users')
.doc(userId)
.collection('namesList')
.where('name', isNotEqualTo: '')
.snapshots();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Josteve |
