我想獲得一個Stream<List<MyModel>>并在以后的StreamBuilder中使用它。
但是我在從Firebase獲取流時遇到了問題。
下面是我獲取資料流的函式:
static Stream<List<Exercise>> getExercisesWithUpdates() {
Stream<QuerySnapshot<Object?>> querySnapshot = _firestore.collection('exercise').snapshots(); //heer kein nullList<Exercise>> test = querySnapshot.map((document) {
return document.docs.map((e) {
Exercise.fromJson(e.data() as Map<String, dynamic> ) 。
}).toList()。
});
return測驗。
}
錯誤資訊。 我想這是由于null的安全問題,但我不確定如何處理這種情況。
在這個例子中,我的Exercise類: uj5u.com熱心網友回復: 你在
標籤:回傳型別'List<Null>'不是'List<Exercise>',符合閉包的背景關系要求。
class Exercise {
String? id;
String? name;
String? imageName;
String? imageUrl;
String? description;
Exercise({required this.id, required this. name, this.imageName, this.imageUrl, this.description})。)
Exercise.empty()。
Exercise.fromJson(Map<String, dynamic> json)
: this(
id: json['id']! as String。
name: json['name']! as String,
imageName: json['imageName']! as String,
imageUrl: json['imageUrl']! as String。
description: json['description']! as String)。)
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
'imageName': imageName。
'imageUrl': imageUrl。
'description': 描述。
};
}
map中缺少一個回傳陳述句:return document.docs.map((e) {
return Exercise.fromJson(e. data() as Map<String, dynamic> )。)
}).toList()。
