I am trying to build a FutureBuilder
but the result of the code gives me Bad state: field dos not exist within the DocumentSnapshotPlatform
error.
I searched around before posting this question and found that this error occurs when the "fields" like users
and username
are not present in firestore but they are clearly present in my firestore. There is a collection named users
containing documents with 'uid' as their name. And every doc contains a property username
in it.
Following is the code of the `FutureBuilder:
FutureBuilder(
future: FirebaseFirestore.instance
.collection("users")
.where(
"username",
isGreaterThanOrEqualTo: searchController.text,
)
.get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(
color: Colors.white,
),
);
}
return ListView.builder(
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(
(snapshot.data! as dynamic).docs[index]["photoUrl"],
),
radius: 16,
),
title: Text(
(snapshot.data! as dynamic).docs[index]["username"],
),
);
},
itemCount: (snapshot.data! as dynamic).docs.length,
);
},
),
Code Explanation: The Appbar
contains a TextFormField
with a controller searchConroller
which will pass the text to future builder in the body section of scaffold. The FutureBuilder
will return a ListView
which will have ListTile
with appropriate username
and photoUrl
(profile picture) taken from snapshot which we will get from the future.
Why is this error occurring then?
Well, The error is really self-explanatory. The fields I was trying to access were all present but I misspelled them and wasted a lot of time reading Firestore Docs and waiting for someone to answer to this question.
Better practice would be (what I can think of): Write down all of your fields in a text file and then write down the fields you are trying to access and compare them, I am sure you will find the discrepancy.
In my case I was trying to access photoUrl
while the field name was photoURL
...
Lesson well-learnt.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments