【问题标题】:The argument type 'Book' can't be assigned to the parameter type 'Map<String, dynamic>'.dart(argument_type_not_assignable)参数类型 'Book' 不能分配给参数类型 'Map<String, dynamic>'.dart(argument_type_not_assignable)
【发布时间】:2021-09-25 06:57:16
【问题描述】:
                 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')),
                    ),

///课本

地图 toMap() { 返回 { '标题':标题, 'user_id':用户ID, “作者”:作者, “笔记”:笔记, 'photo_url': 照片网址, 'published_date':发布日期, “描述”:描述, 'page_count': pageCount, “类别”:类别, }; } }

【问题讨论】:

  • 你能出示_bookCollectionReference声明吗?
  • 我可以看看 '_bookCollectionReference' 是在哪里声明的
  • final _bookCollectionReference = FirebaseFirestore.instance.collection('books');

标签: flutter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2021-08-29
    • 2021-10-02
    • 1970-01-01
    • 2021-09-16
    • 2020-12-15
    • 2021-03-23
    • 2021-05-17
    相关资源
    最近更新 更多