简体   繁体   English

错误:返回类型'Iterable<book> ' 不是 ' 列表<book> ',根据闭包上下文的要求。 在 Flutter</book></book>

[英]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.

相关问题 返回类型“用户?” 不是闭包上下文所要求的“用户”。 Firebase 授权 - The return type 'User?' isn't a 'User', as required by the closure's context. Firebase Auth Flutter Firestore Stream 错误“根据闭包上下文的要求”。 - Flutter Firestore Stream error "as required by the closure's context." 根据闭包上下文的要求,返回类型“Transaction”不是“Future&lt;_&gt;” - The return type 'Transaction' isn't a 'Future<_>', as required by the closure's context 根据闭包上下文的要求,返回类型“LoginPage”不是“void” - The return type 'LoginPage' isn't a 'void', as required by the closure's context Future builder flutter firebase 错误:“Iterable”类型的值<BlogPost> &#39; 不能分配给类型为 &#39;List 的变量<BlogPost> &#39; - Future builder flutter firebase error: A value of type 'Iterable<BlogPost>' can't be assigned to a variable of type 'List<BlogPost>' 没有为 Flutter Firebase 中的“QuerySnapshot”类型错误定义吸气剂“文档” - The getter 'documents' isn't defined for the type 'QuerySnapshot' error in Flutter Firebase 错误:未在 flutter 中为类型“FirebaseMessaging”定义方法“configure” - error: The method 'configure' isn't defined for the type 'FirebaseMessaging' in flutter 没有为“对象”类型定义 getter“文档”- Flutter 错误 - The getter 'documents' isn't defined for the type 'Object' - Flutter Error FLUTTER 未为“对象”类型定义运算符“[]” - FLUTTER The operator '[]' isn't defined for the type 'Object' (Flutter) 错误:参数类型 'String?' 不能分配给参数类型“字符串”,因为“字符串?” 可以为 null 而 'String' 不是 - (Flutter) Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM