【问题标题】:How to fix Bad state: field does not exist within the DocumentSnapshotPlatform如何修复错误状态:DocumentSnapshotPlatform 中不存在字段
【发布时间】:2021-06-21 07:49:20
【问题描述】:

我想在 Flutter 中从 Firestore Firebase 检索数据,但将其显示到列表视图中,然后我按照 youtube 上的教程进行操作。但它显示错误“状态错误:DocumentSnapshotPlatform 中不存在字段”。我不知道如何解决它。谁能帮我解决它?谢谢!!

    Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('TransactionExpense').snapshots(),
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasData) {
                return ListView(
                  children: snapshot.data.docs.map((document) {
                    return Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
    
                        children: [
                          Row(children: [
                            SizedBox(width: 6,),
                            Text(document['category'],style: TextStyle(fontSize: 16,
                                color: primary,
                                fontWeight: FontWeight.w600
                            ),),
                          ],
                          ),
                          SizedBox(height: 10,),
                          Row(children: [
                            SizedBox(width: 6,),
                            Text(document['dates'],style: TextStyle(fontSize: 16,
                                color: primary,
                                fontWeight: FontWeight.w600
                            ),),
                            SizedBox(width: 200,),
                            SizedBox(width: 6,),
                            Text(document['amount'],style: TextStyle(fontSize: 16,
                                color: primary,
                                fontWeight: FontWeight.w600
                            ),),
                          ],
                          ),
                          SizedBox(height: 8,),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              GestureDetector(
                                onTap: (){
                                  //_showDeleteDialog(transactionexpense: transactionexpense);
                                },
                                child: Row(
                                  children: [
                                    Icon(Icons.delete_forever_outlined,
                                      color: Colors.red,
                                    ),
                                    SizedBox(width: 6,),
                                    Text('Delete', style: TextStyle(fontSize: 16,
                                        color: Colors.red,
                                        fontWeight: FontWeight.w600
                                    ), ),
                                  ],
                                ),
                              )
                            ],
                          )
                        ],
                      ),
                    );
                  }).toList(),
                );
              }
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            ),
          );
      }

【问题讨论】:

  • 您能否在所有访问文档对象的地方使用document.get('category'); 而不是使用document['category'] 从firebase 集合中获取值并还原?
  • 哦,是的!谢谢!

标签: flutter google-cloud-platform


【解决方案1】:

您还可以捕获 stateError 并根据需要更新变量。它删除了红屏“DocumentSnapshotPlatform 中不存在该字段”。

try {
  dynamic nested = document.get(FieldPath(['fieldName']));
} on StateError catch (e) {
  nested = 'null';
}

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 2021-09-28
    • 2021-09-27
    • 1970-01-01
    • 2021-10-27
    • 2021-08-24
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    相关资源
    最近更新 更多