简体   繁体   中英

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())));" 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.

Book 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:

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()

listOfDocSnap.map<Book>((docSnap) => Book.fromMap(docSnap.data()))).toList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM