Flutter Cloud Firestore Map <字符串,动态>错误

Joh*_*ser 5 dart firebase flutter google-cloud-firestore

我正在尝试使用Flutter和Firestore构建应用程序。使用StreamBuilder从Firestore加载Collection以便在ListView中显示它时,出现以下错误

The following assertion was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
I/flutter (26287): _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#d5638):

I/flutter (26287): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

I/flutter (26287): where

I/flutter (26287):   _InternalLinkedHashMap is from dart:collection

I/flutter (26287):   Map is from dart:core

I/flutter (26287):   String is from dart:core
Run Code Online (Sandbox Code Playgroud)

这就是我要从 DocumentSnapshot

The following assertion was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
I/flutter (26287): _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#d5638):

I/flutter (26287): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

I/flutter (26287): where

I/flutter (26287):   _InternalLinkedHashMap is from dart:collection

I/flutter (26287):   Map is from dart:core

I/flutter (26287):   String is from dart:core
Run Code Online (Sandbox Code Playgroud)

以及我想如何使用它

class Creator {
  const Creator({this.creatorId, this.name});

  Creator.fromDoc(DocumentSnapshot doc) : this.fromMap(doc.data);

  Creator.fromMap(Map<String, dynamic> map) :
    assert(map.containsKey('creatorId'),
    assert(map.containsKey('name'),
    this ( creatorId: map['creatorId'], name: map['name'] );

  /*

  ...

  */
}
Run Code Online (Sandbox Code Playgroud)

依存关系:

dependencies:
  flutter:
    sdk: flutter

  cloud_firestore: ^0.6.3
  firebase_messaging: ^0.2.4
Run Code Online (Sandbox Code Playgroud)

Firestore仅允许String键和dynamicTimestamp(核心语言类型除外)。该cloud_firestore插件将文档数据保存在中_InternalLinkedHashMap<dynamic, dynamic>。我以为Map里面DocumentSnapshot会是Map<String, dynamic>。我该如何解决?更改所有功能以采用Map<dynamic, dynamic>并假定关键是String解决该问题的方法。

Kev*_*ore 8

我认为这是因为从Firebase返回的Map不是Map<String, dynamic>而是而是Map<dynamic, dynamic>

有关相关问题,请参见https://github.com/flutter/flutter/issues/17417


Mic*_* P. 6

使用Map<String, dynamic>.from ( mapFromFirestore )。这会Map<String, dynamic>根据 的内容创建一个新的mapFromFirestore,如果发现不是 的键,则会抛出错误String