【问题标题】:How can I resolve " Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist"如何解决“状态不佳:无法在 DocumentSnapshotPlatform 上获取不存在的字段”
【发布时间】:2021-11-03 22:13:35
【问题描述】:

当我尝试在我的 appBar 中显示用户的状态时,我遇到了这个问题:

      appBar: AppBar(
    title: StreamBuilder<DocumentSnapshot>(
        stream:  _firestore.collection('users').doc(userMap['uid']).snapshots(),
        builder: (context, snapshot){
          if (snapshot.data != null){
            return Container(
              child: Column(
                children: [
                  Text(userMap['name']),
                  Text(
                    snapshot.data!['status'],
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
            );
          }else{
            return Container();
          }
        }
    ),
    backgroundColor: Colors.white,
    leading: IconButton(
      icon:Icon(Icons.arrow_back_rounded, color: Colors.black,), onPressed: () {
      Navigator.of(context)
          . pushReplacement(MaterialPageRoute(builder: (_) => chatpage()));},
    ),
  ),

在firebase中查看用户状态的所有过程我认为问题出在第一部分,但我展示了其余代码。

class _chatpageState extends State<chatpage> with WidgetsBindingObserver{


Map<String,dynamic>? userMap;
bool isLoading = false;
final FirebaseAuth _auth = FirebaseAuth.instance;
final TextEditingController _search = TextEditingController();
final FirebaseFirestore _firestore = FirebaseFirestore.instance;

@override
void initState(){
super.initState();
WidgetsBinding.instance!.addObserver(this);
setStatus("Online");
}
void setStatus(String status)async{

await _firestore.collection('users').doc(_auth.currentUser!.uid).update({
  "status": status
});

}

@override
void didChangeAppLifecycleState(AppLifecycleState state){
if(state == AppLifecycleState.resumed){
  setStatus("Online");
  //online
 }else{
  setStatus("Offline");
  //offline
 }
 } 

我的方法有一小部分:

    await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
  "name": name,
  "email": email,
  "status": "Unavalible",
  "uid": _auth.currentUser!.uid,
});

如果 somoene 能帮上忙,我会非常感激 :)

【问题讨论】:

    标签: android firebase flutter google-cloud-firestore


    【解决方案1】:

    在 appBar 中将 snapshot.data!['status'] 更改为 snapshot.data.get('status')(如果您确定字段“状态”存在)。

    或者

    dynamic data = snapshot.data.data();
    print(data['status']);
    return Container(
      child: Column(
        children: [
          Text(userMap['name']),
          Text(
            data['status'],
            style: TextStyle(fontSize: 14),
          )
        ],
      ),
    );
    

    【讨论】:

    • 对不起,我已经更新了答案。
    • 更新后的答案在我的系统上运行正常。
    • 不是我的“用于空值的空检查运算符”
    • 这个新错误意味着不应该为 null 的值(prolly 因为它后面有 !)为 null。因此,如果可能,请从旧代码开始(没有此错误并尝试我的代码)。
    • 我自己解决了这个问题很抱歉
    【解决方案2】:

    试试这个

    if (snapshot.data != null){
         final snapshotData = snapshot.data!.data(); //retrive data from snapshot
                return Container(
                  child: Column(
                    children: [
                      Text(userMap['name']),
                      Text(
                        snapshotData["status"]??"", //?? checks if value is null. so you can add emty string or show its updating or undefined
                        style: TextStyle(fontSize: 14),
                      )
                    ],
                  ),
                );
              }else{
                return Container();
              }
            }
    

    【讨论】:

    • 运算符 '[]' 没有为类 'Object?' 定义。当我尝试这个时
    猜你喜欢
    • 2021-08-15
    • 2021-10-26
    • 2021-05-16
    • 2021-05-19
    • 1970-01-01
    • 2022-06-11
    • 2022-09-27
    • 2021-04-27
    • 2021-06-17
    相关资源
    最近更新 更多