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

Viewed 183

Im trying to use a reference from on of my collections in flutter but im getting this error :

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

The relevant error-causing widget was: 
  FutureBuilder<DocumentSnapshot> file:///Users/name/StudioProjects/project/lib/seitenleiste/nachrichten.dart:387:11
When the exception was thrown, this was the stack: 
#0      DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:73:7)
#1      DocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:45:43)
#2      DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:52:41)
#3      MessageBubble.build.<anonymous closure> (package:project/seitenleiste/nachrichten.dart:394:32)
#4      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:775:55)
...
====================================================================================================

======== Exception caught by widgets library =======================================================
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
The relevant error-causing widget was: 
  FutureBuilder<DocumentSnapshot> file:///Users/name/StudioProjects/project/lib/seitenleiste/nachrichten.dart:387:11
====================================================================================================

======== Exception caught by widgets library =======================================================
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
The relevant error-causing widget was: 
  FutureBuilder<DocumentSnapshot> file:///Users/name/StudioProjects/project/lib/seitenleiste/nachrichten.dart:387:11
====================================================================================================

So heres my method :


class MessageBubble extends StatelessWidget {
  MessageBubble(this.message,this.isMe,this.userId,{this.key});
  final Key key;
  final String message;
  final bool isMe;
  String userId;
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment:isMe?MainAxisAlignment.end:MainAxisAlignment.start,
    children:<Widget>[ Container(
      decoration: BoxDecoration(
        color:isMe?Colors.grey[300]:Theme.of(context).accentColor,
        borderRadius: BorderRadius.circular(12),

      ),
      width: 140,
      padding: EdgeInsets.symmetric(
          vertical: 10,horizontal: 16),
      margin:EdgeInsets.symmetric(
          vertical: 4,horizontal: 8
      ),
      child:Column(
        children: <Widget>[
          FutureBuilder(
            future: FirebaseFirestore.instance.collection('myprofilsettings').doc('userId').get(),
            builder: (context, snapshot) {
              if(snapshot.connectionState==ConnectionState.waiting){
                return Text('Loading...');
              }
              return Text(
                  snapshot.data['username'],
                  style:TextStyle(fontWeight:FontWeight.bold ));
            }
          ),
          Text(
          message,style:
        TextStyle(color:isMe?Colors.black:Theme.of(context).
        accentTextTheme.title.color
        ),
        ),
        ]
      )  )
]

    );

  }
}

With 'userId' property im trying to reference on 'userId' in my chat collection. What I want is to use the 'username' property from my 'meinprofilsettings' collection and use it in my 'chat' collection. So im trying to get the username of the correct user. I will storage a picture of my databse so you can see how do I wan it. So when you open both picture you will see the 'userId' and the id of the document in 'meinprofilsettings' is on one document the same as the 'userId' and if that so, then I wanna get the username and displayed. Hope you understand . If you don't please leave a comment .

And heres my databse class:

class DatbaseService{

  final String uid;
  DatbaseService({this.uid});
  //collection reference
  final CollectionReference myprofilsettings = FirebaseFirestore.instance.collection('meinprofilsettings');

  Future updateUserData(String email,String fullname,String password,String url,String username)async{
    return await myprofilsettings.doc(uid).set({
      'email':email,
      'fullname':fullname,
      'password':password,
      'url':url,
      'username':username,
    });

  }

  //profil list from snapshot
  List<myprofil> _myprofillistFromSnapshot(QuerySnapshot snapshot){
    return snapshot.docs.map((doc){
      return myprofil(
        email: doc.data()['email']??'',
        fullname: doc.data()['fullname']??'',
        password: doc.data()['password']??'',
        url: doc.data()['url']??'',
        username: doc.data()['username']??'',
      );
    }).toList();
  }
  //userData from snapshot
  UserData _userDataFromSnapshot(DocumentSnapshot snapshot){
    return UserData(
      uid: uid,
      email: snapshot.data()['email'],
      fullname: snapshot.data()['fullname'],
      password: snapshot.data()['password'],
      url: snapshot.data()['url'],
      username: snapshot.data()['username'],
    );

  }
  //get myprofilsettings stream we need that to work with the values
  Stream<List<myprofil>> get settings{
    return myprofilsettings.snapshots().map(_myprofillistFromSnapshot);
  }

  //get user doc stream
Stream<UserData> get userData{
    return myprofilsettings.doc(uid).snapshots().map(_userDataFromSnapshot);
}
}

1 Answers

you need to use:

FutureBuilder(
   future: 
FirebaseFirestore.instance.collection('myprofilsettings').doc(userId).get(),
            builder: (context, snapshot) { ....///etc

you were using 'userId' as a string, but this means that you want firebase to go get the document with id == 'userId' literally, not the value of your user id.

Related