异常发生.状态错误(坏状态:字段在DocumentSnapshotPlatform中不存在)-当我请求Flutter F



我需要从Firestore中的数组中获取数据。

我得到一个错误:

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform)

当我在firestore数据库上运行以下查询时:

Future getData() async {
await FirebaseFirestore.instance
.collection("users")
.get()
.then((QuerySnapshot? querySnapshot) {
querySnapshot!.docs.forEach((doc) {
alldata = doc["errors"];
});
});
}

如何正确地进行此查询?

试试这个:

Future getData() async {
final snapshot = await FirebaseFirestore.instance
.collection("users")
.get();
querySnapshot!.docs.forEach((doc) {
alldata = (doc.data() as Map<String, dynamic>)["errors"];
});
}

解决!

Future getData() async {
final docRef =
await FirebaseFirestore.instance.collection("users").doc(user.uid);
docRef.get().then(
(DocumentSnapshot doc) {
alldata = (doc.data() as Map<String, dynamic>)["errors"];
print('Получен массив данных: ${alldata}');
print('Получен массив данных: ${alldata.first}');
},
onError: (e) => print("Error getting document: $e"),
);
}

相关内容