【问题标题】:getting error :Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist出现错误:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段
【发布时间】:2021-06-17 20:34:26
【问题描述】:

我试图在颤振中使用我的集合中的一个引用,但我得到了这个错误:

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
====================================================================================================

这是我的方法:


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
        ),
        ),
        ]
      )  )
]

    );

  }
}

使用“userId”属性,我试图在我的聊天集合中引用“userId”。我想要的是使用我的“meinprofilsettings”集合中的“用户名”属性,并在我的“聊天”集合中使用它。所以我试图获取正确用户的用户名。我将存储我的数据库的图片,以便您了解我如何使用它。 因此,当您打开两张图片时,您会看到“userId”,并且“meinprofilsettings”中的文档 ID 位于与“userId”相同的一个文档上,如果是这样,那么我想获取用户名并显示。希望你能理解 。如果您不喜欢,请发表评论。

这是我的数据库类:

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);
}
}

【问题讨论】:

    标签: database firebase flutter google-cloud-firestore


    【解决方案1】:

    你需要使用:

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

    您使用 'userId' 作为字符串,但这意味着您希望 firebase 从字面上获取 id == 'userId' 的文档,而不是您的用户 id 的值。

    【讨论】:

    • 谢谢你。祝你有个愉快的夜晚!
    猜你喜欢
    • 2021-10-26
    • 2021-05-16
    • 2021-05-19
    • 2021-04-27
    • 2021-09-28
    • 2021-09-27
    • 2021-05-16
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多