gpt4 book ai didi

firebase - Flutter Cloud Firestore Map 错误

转载 作者:IT老高 更新时间:2023-10-28 12:43:38 26 4
gpt4 key购买 nike

我正在尝试使用 Flutter 和 Firestore 构建应用程序。使用 StreamBuilder 从 Firestore 加载集合以在 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

这就是我想要从 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

Firestore 仅允许 String键和 dynamic值是,Timestamp 除外,核心语言类型。 cloud_firestore插件将文档数据保存在 _InternalLinkedHashMap<dynamic, dynamic> 中.我以为 Map全部DocumentSnapshot将是 Map<String, dynamic> .我该如何解决这个问题?更改所有功能以获取 Map<dynamic, dynamic>并假设 key 是 String是一个相当丑陋的解决方法。

最佳答案

我相信这是因为从 Firebase 返回的 map 不是 Map<String, dynamic>而是 Map<dynamic, dynamic> .

https://github.com/flutter/flutter/issues/17417相关问题。

关于firebase - Flutter Cloud Firestore Map<String, dynamic> 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50159766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com