I've been struggling with an error that is apparently due to new changes of DocumentSnapshot that I'm trying to use in a FutureBuilder you can see how i'm using it below :

body: FutureBuilder(
  future: usersRef.doc(widget.userId).get(),
  builder: ( context,  snapshot) {
    List<Widget> children;
    if (!snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    else if (snapshot.hasData) {
      UserModel user = UserModel.fromDoc(snapshot.data);
factory UserModel.fromDoc(DocumentSnapshot doc) {
  return UserModel(
    id: doc.id,
    name: doc['name'],
    username: doc['username'],
    password: doc['password'],
    profileImageUrl: doc['profileImageUrl'],
    email: doc['email'] ,
    userIds: doc['userIds'] ?? '',
  );
}

I encounter this error:

The following StateError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#46a8d):
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

I already tried to solve it using the solution that I found on this github issue ( https://github.com/FirebaseExtended/flutterfire/issues/3826) but It didn't really solve it.
I'll be so appreciative if any of you help me with this Thanks


Solution 1: Quang Duy Tran

I got the same issue, haven't found the way to check field is exist before access to it, so my workaround like below

factory UserChat.fromDocument(DocumentSnapshot doc) {
  String aboutMe = "";
  String photoUrl = "";
  String nickname = "";
  try {
    aboutMe = doc.get('aboutMe');
  } catch (e) {}
  try {
    photoUrl = doc.get('photoUrl');
  } catch (e) {}
  try {
    nickname = doc.get('nickname');
  } catch (e) {}
  return UserChat(
    id: doc.id,
    photoUrl: photoUrl,
    nickname: nickname,
    aboutMe: aboutMe,
  );
}


Solution 2: Sarthak Kumar Mishra

Just simply add the snapshot type in FutureBuilder,

just write this FutureBuilder<DocumentSnapshot>(...) and it will work perfectly fine.