Nush
Nush

Reputation: 55

Bad state: field does not exist within the DocumentSnapshotPlatform. Flutter and firebase

I'm trying to build a list from a collection from firebase database but I'm running into a problem with the stream builder snapshots.

StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance.collection("users").snapshots().where((event) => true),
          builder: (context, snapshot) {
            if (snapshot.hasData && snapshot.data != null) {
              return ListView.builder(
                itemBuilder: (listContext, index) =>
                    buildItem(snapshot.data.docs[index]),
                itemCount: snapshot.data.docs.length,
              );
            }

            return Container();
          },
        ));
  }

  buildItem(doc) {
    return (userId != doc['id'])
        ? GestureDetector(
      onTap: () {
        //Navigator.push(context, MaterialPageRoute(builder: (context) => Page(docs: doc)));
      },
      child: Card(
        color: Colors.black,
        child: Container(
          height: 60,
          child: Center(
            child: Text(doc['name'],
              style: TextStyle(
                color: Colors.white,
                fontSize: 20,
              ),
            ),
          ),
        ),
      ),
    )
        : Container();
  }

Here is the whole error:

======== Exception caught by widgets library =======================================================
The following StateError was thrown building:
Bad state: field does not exist within the DocumentSnapshotPlatform

When the exception was thrown, this was the stack: 
#0      DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:86:7)
#1      DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:104:41)
#2      DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:120:26)
#3      _JsonDocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:92:48)
#4      _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40)
...
====================================================================================================

The builder is building the list but its giving a red error section directly below it.

Upvotes: 0

Views: 353

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

The doc in your buildItem method is a DocumentSnapshot object, which doesn't have an [] defined as far as I know.

I think you're missing a call to data():

userId != doc.data()['id']

If you get another error message after this change, please search for it on Stack Overflow first as I know there have been some recent changes in how types are exposed from a document snapshot.

Upvotes: 1

Related Questions