Firebase Cloudstore 發生以下錯誤。我無法將串列函式映射到流串列 todo 引數型別“串列?Function(QuerySnapshot<Object?>)' 不能分配給引數型別 'List Function(QuerySnapshot<Object?>)'。
匯入“飛鏢:數學”;
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:todolist/models/model.dart';
class databaseService {
CollectionReference todosCollection =
FirebaseFirestore.instance.collection("Todos");
Future createNewTools(String title) async {
await todosCollection.add({
"title": title,
"iscomplete": false,
});
}
Future completTask(id) async {
await todosCollection.doc(id).update({"iscomplete": true});
}
Future removeTodo(uid) async {
await todosCollection.doc(uid).delete();
}
List<Todolist>? todoFromFirestore(QuerySnapshot snapshot) {
if (snapshot != null) {
return snapshot.docs.map((e) {
return Todolist(
iscomplete: e["iscomplete"],
title: e["title"],
id: e.id,
);
}).toList();
} else {
return null;
}
}
Stream<List<Todolist>> listTodo(){
return todosCollection.snapshots().map(todoFromFirestore); //error
}
}
和 todolist 模型檔案:
class Todolist {
String id;
String title;
String iscomplete;
Todolist({required this.id, required this.title, required this.iscomplete, isComplete});
}
錯誤:

uj5u.com熱心網友回復:
List<Todolist> todoFromFirestore(QuerySnapshot snapshot) {
if (snapshot != null) {
return snapshot.docs.map((e) {
return Todolist(
iscomplete: e["iscomplete"],
title: e["title"],
id: e.id,
);
}).toList();
} else {
return [];
}
}
可空串列導致錯誤。嘗試這個...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355069.html
標籤:火力基地 扑 谷歌云firestore
上一篇:如何在顫振中回傳捕獲例外
下一篇:在iPhone模擬器上運行的flutter停留在“Xcode構建完成”上。(它在android模擬器中運行良好)
