Following error occured for Firebase Cloudstore . I cant map a list function to Stream list todo The argument type 'List? Function(QuerySnapshot<Object?>)' can't be assigned to the parameter type 'List Function(QuerySnapshot<Object?>)'.
import 'dart:math';
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
}
}
and the todolist model file:
class Todolist {
String id;
String title;
String iscomplete;
Todolist({required this.id, required this.title, required this.iscomplete, isComplete});
}
CodePudding user response:
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 [];
}
}
A nullable List is causing the error. Try This...