参数类型 'Book' 不能分配给参数类型 'Map<String, dynamic>'.dart(argument_type_not_assignable)

时间:2021-07-16 13:06:41

标签: flutter

                 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 toMap() { 返回 { '标题':标题, 'user_id': 用户 ID, '作者':作者, '笔记':笔记, 'photo_url': photoUrl, 'published_date':已发布日期, '描述':描述, 'page_count':pageCount, “类别”:类别, }; } }

1 个答案:

答案 0 :(得分:0)

您忘记通过调用 .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(),
);