我在 flutter 中运行单元测试,出现错误:
类型“List”不是类型“List
相关代码片段如下,我试图从 firebase 读取数据。
factory TimerSessionModel.fromSnapshot(
DocumentSnapshot<Map<String, dynamic>> snapshot) {
final data = snapshot.data()!;
return TimerSessionModel(
selfRef: snapshot.reference,
activeExecutionRef: data['activeExecutionRef'],
autoStartNextTimer: data['autoStartNextTimer'] as bool,
ownerRef: data['ownerRef'],
timerRefs: data['timerRefs'],
title: data['title'] as String,
);
}
这里timerRefs是一个文档引用列表,问题所指向的地方。
我尝试运行单元测试并给出了如上所述的错误
您需要将列表类型转换为
factory TimerSessionModel.fromSnapshot(
DocumentSnapshot<Map<String, dynamic>> snapshot) {
final data = snapshot.data()!;
// Convert List<dynamic> to List<DocumentReference<Map<String, dynamic>>>
List<DocumentReference<Map<String, dynamic>>> timerRefs =
(data['timerRefs'] as List<dynamic>)
.map((ref) => ref as DocumentReference<Map<String, dynamic>>)
.toList();
return TimerSessionModel(
selfRef: snapshot.reference,
activeExecutionRef: data['activeExecutionRef'],
autoStartNextTimer: data['autoStartNextTimer'] as bool,
ownerRef: data['ownerRef'],
timerRefs: timerRefs,
title: data['title'] as String,
);
}