Flutter Cloud Firestore Map< String,dynamic>错误 [英] Flutter Cloud Firestore Map<String, dynamic> error
问题描述
我正在尝试使用Flutter和Firestore构建应用程序.使用StreamBuilder从Firestore加载集合以在ListView中显示集合时,出现以下错误
I am trying to build an app using Flutter and Firestore. When loading a Collection from Firestore using StreamBuilder to display it in a ListView, I get the following error
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
这就是我要从DocumentSnapshot
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'] );
/*
...
*/
}
我想如何使用它
return Scaffold(
appBar: AppBar(title: new Text('Creators')),
body: StreamBuilder<QuerySnapshot>(
stream: CreatorRepo.getCreators().map<List<Creator>>((creators) {
return creators.documents.map<Creator>((c) => Creator.fromSnapshot(c)).toList();
}),
builder: (BuildContext context, snapshot) {
if ( snapshot.hasData ) {
return ListView.builder(
itemCount: snapshot.data.length,
builder: (context, index) {
final creator = snapshot.data[index];
return ExpansionTile(
title: Text(creator.name),
children: [
Text(creator.creatorId),
],
);
},
);
}
return const CircularProgressIndicator();
},
),
);
依赖项:
dependencies:
flutter:
sdk: flutter
cloud_firestore: ^0.6.3
firebase_messaging: ^0.2.4
除Timestamp
外,
Firestore仅允许String
键和dynamic
值是核心语言类型. cloud_firestore
插件将文档数据保存在_InternalLinkedHashMap<dynamic, dynamic>
中.我认为所有DocumentSnapshot
中的Map
将是Map<String, dynamic>
.我该如何解决?更改所有功能以采用Map<dynamic, dynamic>
并假定键为String
是解决该问题的相当丑陋的方法.
Firestore only allows for String
keys and dynamic
values being, with the exception of Timestamp
, core language types. The cloud_firestore
plugin keeps document data in an _InternalLinkedHashMap<dynamic, dynamic>
. I thought that the Map
inside all DocumentSnapshot
would be Map<String, dynamic>
. How can I work around this? Changing all functions to take Map<dynamic, dynamic>
and assume the key is a String
is a rather ugly workaround to the problem.
推荐答案
我相信这是因为从Firebase返回的地图不是Map<String, dynamic>
而是Map<dynamic, dynamic>
.
I believe this is because the Map returned from Firebase is not a Map<String, dynamic>
but instead Map<dynamic, dynamic>
.
请参见 https://github.com/flutter/flutter/issues/17417 有关的问题.
这篇关于Flutter Cloud Firestore Map< String,dynamic>错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!