[英]error: The return type 'Iterable<Book>' isn't a 'List<Book>', as required by the closure's context. in Flutter
"listOfDocSnap.map((docSnap) => Book.fromMap(docSnap.data())));" “listOfDocSnap.map((docSnap) => Book.fromMap(docSnap.data())));” I am getting error on line:
我在网上遇到错误:
error: The argument type 'Object?'错误:参数类型“对象?” can't be assigned to the parameter type 'Map<dynamic, dynamic>'.
不能分配给参数类型“地图<动态,动态>”。
and和
error: The return type 'Iterable' isn't a 'List', as required by the closure's context.错误:根据闭包上下文的要求,返回类型“Iterable”不是“List”。
Book class:预订 class:
class Book {
final String bookName;
final String id;
final String authorName;
final Timestamp publishedDate;
Book(
{required this.bookName,
required this.id,
required this.authorName,
required this.publishedDate});
Map<String, dynamic> toMap() => {
'id': id,
'bookName': bookName,
'authoName': authorName,
'publishedDate': publishedDate,
};
factory Book.fromMap(Map map) => Book(
bookName: map['bookName'],
id: map['id'],
authorName: map['authorName'],
publishedDate: map['publishedDate']);
}
View model class:查看 model class:
class BooksViewModel extends ChangeNotifier {
Database _database = Database();
Stream<List<Book>> getBookList() {
const String booksRef = 'books';
Stream<List<DocumentSnapshot>> streamListDocument =
_database.getBooks(booksRef).map((querySnapshot) => querySnapshot.docs);
Stream<List<Book>> streamListOfBook = streamListDocument.map(
(listOfDocSnap) =>
***listOfDocSnap.map((docSnap) => Book.fromMap(docSnap.data())));***
return streamListOfBook;
}
}
map returns Iterable so you need to call.toList() map 返回 Iterable 所以你需要 call.toList()
listOfDocSnap.map<Book>((docSnap) => Book.fromMap(docSnap.data()))).toList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.