[英]Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String?>'
I got an error when I fetch the array data from FireStore.从 FireStore 获取数组数据时出现错误。 I added "blockUid" field on FireStore.
我在 FireStore 上添加了“blockUid”字段。 but It's not working, despite the others are all working.
但它不工作,尽管其他人都在工作。
github: https://github.com/ATUSHIKADOWAKI/dance_4_life/tree/main/lib github: https://github.com/ATUSHIKADOWAKI/dance_4_life/tree/main/lib
(main_model.dart) (main_model.dart)
Future<void> fetchEvents() async {
final docs = await FirebaseFirestore.instance
.collection('event')
.orderBy('timestamp', descending: true)
.get();
final events = docs.docs.map((doc) => Events(doc)).toList();
this.events = events;
notifyListeners();
}
(events.dart) (事件.dart)
class Events {
String? eventId;
String? title;
String? date;
String? imgURL;
String? detail;
//array is here.
List<String?> blockUid = [];
String? eventPlace;
String? eventAddress;
String? eventCategory;
String? eventPrice;
String? eventGenre;
Events(DocumentSnapshot doc) {
eventId = doc.id;
title = doc['title'];
eventPlace = doc['eventPlace'];
eventAddress = doc['eventAddress'];
eventCategory = doc['eventCategory'];
eventPrice = doc['eventPrice'];
eventGenre = doc['eventGenre'];
date = doc['date'];
imgURL = doc['imgURL'];
detail = doc['detail'];
blockUid = doc['blockUid'];
}
}
You need to change this line您需要更改此行
blockUid = doc['blockUid'];
to至
blockUid = doc['blockUid'] as List<String>;
Make sure all the data in the blockUid list in Firestore is String and try this:确保 Firestore 中 blockUid 列表中的所有数据都是字符串,然后试试这个:
Future<void> fetchEvents() async {
final docs = await FirebaseFirestore.instance
.collection('event')
.orderBy('timestamp', descending: true)
.get();
final events = docs.docs.map((doc) => Events.fromDoc(doc)).toList();
this.events = events;
notifyListeners();
}
class Events {
Events({
this.eventId,
this.title,
this.date,
this.imgURL,
this.detail,
this.blockUid,
this.eventPlace,
this.eventAddress,
this.eventCategory,
this.eventPrice,
this.eventGenre,
});
String? eventId;
String? title;
String? date;
String? imgURL;
String? detail;
//array is here.
List<String>? blockUid;
String? eventPlace;
String? eventAddress;
String? eventCategory;
String? eventPrice;
String? eventGenre;
factory Events.fromDoc(DocumentSnapshot doc) => Events(
eventId: doc.id,
title: doc['title'],
eventPlace: doc['eventPlace'],
eventAddress: doc['eventAddress'],
eventCategory: doc['eventCategory'],
eventPrice: doc['eventPrice'],
eventGenre: doc['eventGenre'],
date: doc['date'],
imgURL: doc['imgURL'],
detail: doc['detail'],
blockUid: doc['blockUid'],
);
}
please replace请更换
blockUid = doc['blockUid'];
with this有了这个
blockUid = List<String>.from(doc["data"].map((x) => x) ?? [])
You only need to do Replace this List<dynamic> blockUid = [];
你只需要替换这个
List<dynamic> blockUid = [];
Cast individual item to String, something like this:将单个项目转换为字符串,如下所示:
Events(DocumentSnapshot doc) {
eventId = doc.id;
title = doc['title'];
...
blockUid = doc['blockUid'].map((item) => item as String).toList()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.