'.dart(argument_type_not_assignable),flutter"/>

首页 > 解决方案 > 参数类型“书”不能分配给参数类型“地图”'.dart(argument_type_not_assignable)

问题描述

                 actions: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextButton(
                          onPressed: () {
                              _bookCollectionReference.add(Book(
                              userId: FirebaseAuth.instance.currentUser.uid,
                              title: book.title,,
                              author: book.author,
                              photoUrl: book.photoUrl,
                              publishedDate: book.publishedDate,
                              description: book.description,
                              pageCount: book.pageCount,
                              categories: book.categories,
                            ));
                          },
                          child: Text('Save')),
                    ),

///课本

Map<String, dynamic> toMap() { return { 'title': title, 'user_id': userId, 'author': author, 'notes': notes, 'photo_url': photoUrl, 'published_date': publishedDate, 'description ':描述,'page_count':pageCount,'categories':类别,}; } }

标签: flutter

解决方案


您忘记通过调用 .toMap() 将 Dart 类转换为 Map (json)

在 onPressed 方法中尝试以下代码:

_bookCollectionReference.add(
  Book(
    userId: FirebaseAuth.instance.currentUser.uid,
    title: book.title,,
    author: book.author,
    photoUrl: book.photoUrl,
    publishedDate: book.publishedDate,
    description: book.description,
    pageCount: book.pageCount,
    categories: book.categories,
  ).toMap(),
);

推荐阅读