field dos not exist within the DocumentSnapshotPlatform

Lovish Singla

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?

Lovish Singla

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.

edited at
0

Comments

0 comments
Login to comment

Related

Flutter: Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform

Problem with stream and firebase Bad state: field does not exist within the DocumentSnapshotPlatform

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform) on flutter

Error: Bad state : field does not exist within the DocumentSnapshotPlatform

Bad state: field does not exist within the DocumentSnapshotPlatform error

flutter Bad state: field does not exist within the DocumentSnapshotPlatform

Flutter: Bad state: field does not exist within the DocumentSnapshotPlatform in streambuilder

Flutter - "Bad state: field does not exist within the DocumentSnapShotPlatform"

Bad state: field does not exist within the DocumentSnapshotPlatform flutter error even though field exists

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). How can I get a doc from firestore?

How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter FutureBuilder

Bad state: field does not exist within the DocumentSnapshotPlatform. Giving this error but don't know where the error is

I tried to display 'DateTime' on flutter UI get from firestore but show " Bad state: field does not exist within the DocumentSnapshotPlatform "

Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist, Firebase Flutter

How can I resolve " Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist"

Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist provider

Mongo query if field does not exist. If exists, must be within a range

Check Field Exist in Pymongo

Field does not exist ODOO

Logstash define field within a field

If field does not exist, query by a different field

Finding field exist or not through reflection

MongoDB: find a field exist in document

BigQuery check if a nested field exist

project field not exist by default value

Add field if not exist to document in Mongo

Concat nested field values if they exist

Postgres order row for exist field

TOP Ranking

HotTag

Archive