我有一个记录类来解析来自Firestore的对象.我的课程精简版如下所示:
class BusinessRecord {
BusinessRecord.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
name = map['name'] as String,
categories = map['categories'] as List<String>;
BusinessRecord.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
final String name;
final DocumentReference reference;
final List<String> categories;
}
这可以很好地编译,但当它运行时,会出现运行时错误:
type List<dynamic> is not a subtype of type 'List<String>' in type cast
个
如果我只用categories = map['categories'];
,我会得到一个编译错误:The initializer type 'dynamic' can't be assigned to the field type 'List<String>'
.
我的Firestore对象上的categories
是一个字符串列表.我该如何恰当地铸造这个呢?
编辑:以下是当我使用实际编译的代码时异常的样子: