当我尝试从 firestore 获取数据时,发生此异常。 _TypeError(“List”类型不是“String”类型的子类型) 我找不到问题的原因。请帮忙。
class ActfMiddleWare {
///Fetches LocationData From Firestore and Save it in Store;
locationIndexMiddleware() {
return (Store<AppState> store, action, NextDispatcher next) async {
if (action is GetLocationIndex) {
Future<DocumentSnapshot<Map<String, dynamic>>> locationIndex =
FirebaseFirestore.instance
.collection('locations')
.doc('locationIndex')
.get();
await locationIndex.then((docSnapshot) {
if (docSnapshot.data() != null) {
List temp = docSnapshot.data()!['locations'];
store.dispatch(SaveLocationIndex(payload: temp));
}
});
}
next(action);
};
}
}
答案 0 :(得分:0)
错误是您在列表中投射字符串数据。
检查数据库中的数据。
这是将列表更改为字符串所需的修复。
String temp = docSnapshot.data()!['locations'];
这是完整的代码
class ActfMiddleWare {
///Fetches LocationData From Firestore and Save it in Store;
locationIndexMiddleware() {
return (Store<AppState> store, action, NextDispatcher next) async {
if (action is GetLocationIndex) {
Future<DocumentSnapshot<Map<String, dynamic>>> locationIndex =
FirebaseFirestore.instance
.collection('locations')
.doc('locationIndex')
.get();
await locationIndex.then((docSnapshot) {
if (docSnapshot.data() != null) {
String temp = docSnapshot.data()!['locations']; -->Here is the change
store.dispatch(SaveLocationIndex(payload: temp));
}
});
}
next(action);
};
}
}