Flutterfire: The following StateError was thrown building: Bad state: field does not exist within the DocumentSnapshotPlatform

Created on 9 Oct 2020  ยท  12Comments  ยท  Source: FirebaseExtended/flutterfire

Stale customer-response

Most helpful comment

This is probably related to recent changes in DocumentSnapshot, your factory is looking for the data map, which used to be in doc, but nows its in doc.data() - except DocumentID

So for your factory, one way to change it would be to adjust to:

factory Userr.fromDocument(DocumentSnapshot doc, Map docdata) {
return Userr(
id: doc.id,
email: docdata['email'],
username: docdata['username'],
url: docdata['url'],
profileName: docdata['profileName'],
bio: docdata['bio'],
subject1: docdata['subject1'],
subject2: docdata['subject2'],

and when you call it, you'll need to do something like:

DocumentSnapshot doc = await usersRef.doc(u.uid).get();
Map _docdata = doc.data();
      newUserrr = Userr.fromDocument(doc, _docdata);

All 12 comments

@Chandrakant123456
Please provide flutter doctor -v and a minimal complete reproducible code sample that shows the issue. Just stating the error without any description isn't helpful.

Error Report ๐Ÿ‘:

E/flutter (18312): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform
E/flutter (18312): #0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:82:7)
E/flutter (18312): #1 DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:98:41)
E/flutter (18312): #2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:113:26)
E/flutter (18312): #3 DocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:49:43)
E/flutter (18312): #4 DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:56:41)
E/flutter (18312): #5 new Userr.fromDocument (package:fast_access/models/user.dart:100:20)

Error in Code:

factory Userr.fromDocument(DocumentSnapshot doc) {
return Userr(
id: doc.documentID,
email: doc['email'],
username: doc['username'],
url: doc['url'],
profileName: doc['profileName'],
bio: doc['bio'],
_subject1: doc['subject1'],_
subject2: doc['subject2'],

This is probably related to recent changes in DocumentSnapshot, your factory is looking for the data map, which used to be in doc, but nows its in doc.data() - except DocumentID

So for your factory, one way to change it would be to adjust to:

factory Userr.fromDocument(DocumentSnapshot doc, Map docdata) {
return Userr(
id: doc.id,
email: docdata['email'],
username: docdata['username'],
url: docdata['url'],
profileName: docdata['profileName'],
bio: docdata['bio'],
subject1: docdata['subject1'],
subject2: docdata['subject2'],

and when you call it, you'll need to do something like:

DocumentSnapshot doc = await usersRef.doc(u.uid).get();
Map _docdata = doc.data();
      newUserrr = Userr.fromDocument(doc, _docdata);


Hey @Chandrakant123456. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

I have the same issue please someone help me?

Hey @jschnip , Thank you so much for your answer ! I was really having hard time due to recent changes in FlutterFire plugins !
One error was parsing the data using fromDoc method like this ! Thanks again mate.

I'm getting this error when i try to read a field that doesn't exist yet in the document. Previously it simply returned null and i think that was better.

I'm getting this error when i try to read a field that doesn't exist yet in the document. Previously it simply returned null and i think that was better.

@Alfie-AD Hey man facing same error, what does it return now? As I need it for an if statement.

Doesn't return anything, only throws that exception. I checked that the field existed first to try to work around
'doc.data().contains(the_field)' will return whether the the field exists or not. Pair it with the value you want in the if statement: 'if(doc.data().contains(the_field) && doc.data()[the_field] == the_value){}'

@Alfie-AD I have tried and it did not work, so I had to reverse it, here is a screenshot of the code section please advice accordingly on how to fix.
Capture

In recent cloud firestore release the data getter was changed to a method data().
Try final profileDocs = profileSnapshot.data(); ...
if you recently updated your package, have a look at the changelog, there are a few more surprises there.

I get this error too. I am building a social media app. I was now working to customize and add more fields to user profile so i added 10 more strings of data i want to collect. I followed the same steps i used to create a blank field for the users to be able to add information:. Everything was working fine before the changes

Debug Error log

โ•โ•โ•โ•โ•โ•โ•โ• Exception caught by widgets library โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was
StreamBuilder<QuerySnapshot>
lib\pages\timeline.dart:70
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

The Code Snippet from timeline.dart:70 reads:

buildUsersToFollow() {
  return StreamBuilder(
    stream: usersRef.orderBy('timestamp', descending: true)
    .limit(50)
    .snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return circularProgress(context);
      }
      List<UserResult> userResults = [];
      snapshot.data.docs.forEach((doc) {
        User user = User.fromDocument(doc);
        final bool isAuthUser = currentUser.id == user.id;
        final bool isFollowingUser = followingList.contains(user.id);
        // remove auth user from recommended list
        if (isAuthUser) {
          return;
        } else if (isFollowingUser) {
          return;
        } else {
          UserResult userResult = UserResult(user);
          userResults.add(userResult);
        }
        });

        return Container(
          color: Colors.grey.withOpacity(0.2),
          child: Column(
            children: <Widget>[
              Container(
                padding: EdgeInsets.all(12.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.person_add_alt_1_outlined,
                      color: Theme.of(context).primaryColor,
                      size: 20,
                    ),
                    SizedBox(width: 6.0,),
                    Text(
                      "Doctors to follow",
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                        fontSize: 20,
                      ),
                    ),
                  ],
                ),
              ),
              Column(
                children: userResults
              ),
            ],
          ),
        );

    },
    );
}

  @override
  Widget build(context) {
    return Scaffold(
      appBar: header(context, isAppTitle: true),
      body: RefreshIndicator(
        onRefresh: () => getTimeline(),
        child: buildTimeline(),
      ),
    );
  }
}

Everything was working ok until i made the customization and look to add more

snapshot.data.docs.forEach((doc)

Instead, try to do snapshot.data.data().docs.forEach((doc)
I was also getting the same error and I tried with the updated method by adding .data() and it worked fine.


Hey @Chandrakant123456. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

Was this page helpful?
0 / 5 - 0 ratings