如何修复错误状态:DocumentSnapshotPlatform中不存在字段



我想在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(),
);
}
),
);
}

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

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

最新更新