【问题标题】:How to add different data types to firestore in flutter如何在flutter中向firestore添加不同的数据类型
【发布时间】:2018-08-28 08:02:12
【问题描述】:

在 Firebase 控制台中,您可以将布尔值添加为字段,甚至是 int。我试图通过代码添加这样的数据类型,它会产生错误。我的代码看起来像

 Int Score;
 Bool completion;

void addDetails() {
 Map<dynamic, dynamic> data = <dynamic, dynamic>{
  Score: 1.0,
  completion: false,
};
documentReference.setData(data).whenComplete(() {
  print('Uploaded');
  }).catchError((e) => print(e));
}

控制台错误

  I/flutter ( 3284): Another exception was thrown: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

【问题讨论】:

  • “它会产生一个错误。”请添加错误输出
  • 添加了控制台错误

标签: firebase flutter google-cloud-firestore


【解决方案1】:

没有引号

Map<dynamic, dynamic> data = <dynamic, dynamic>{
  'Score': 1.0,
  'completion': false,
};

Firestore 仅接受有效的 JSON,并且需要引号。 在 JS 中它们可以省略,因为所有对象键都是字符串,但在 Dart 中它们也可以是其他类型(但不适用于 JSON)。

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2020-09-12
    • 1970-01-01
    • 2021-01-08
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2020-09-07
    相关资源
    最近更新 更多