颤振运行时发生异常。_TypeError(类型 'List<dynamic>' 不是类型"字符串"的子类型)



当我尝试从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);
};
}
}

错误是您在列表中转换字符串数据。

检查数据库中的数据

这里是你需要改变列表为字符串的修复。

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);
};
}
}

相关内容