【发布时间】:2019-02-14 22:03:49
【问题描述】:
您好,我想将实时数据库中的快照反序列化为 Company 对象并将其添加到 _companies 列表中。 但我不断收到错误...
这是我目前所拥有的:
List<Company> _companies = [];
@override
void initState() {
// TODO: implement initState
super.initState();
getItems().then((list){
print("Now the list is here");
setState(() {
for (int i=0; i < list.length; i++) {
Map<String, dynamic> map = list[i];
Company company = new Company.fromMap(map);
_companies.add(company);
}
});
});
}
static Future<List> getItems( ) async {
Completer<List> completer = new Completer<List>();
FirebaseDatabase.instance
.reference()
.child("Companies")
.once()
.then( (DataSnapshot snapshot) {
List map = snapshot.value; //It fails here
completer.complete(map);
} );
return completer.future;
}
这是我的公司课程:
class Company {
String key;
String category;
String company_name;
String company_url;
String country;
String description;
String email;
String faq_url;
String instagram_url;
String logo_url_image;
String p_category;
String parent_company_ok;
String timestamp;
Company();
Company.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.key,
category = snapshot.value['category'],
company_name = snapshot.value['company_name'],
company_url = snapshot.value['company_url'],
country = snapshot.value['country'],
description= snapshot.value['description'],
email = snapshot.value['email'],
faq_url = snapshot.value['faq_url'],
instagram_url = snapshot.value['instagram_url'],
logo_url_image = snapshot.value['logo_url_image'],
p_category = snapshot.value['p_category'],
parent_company_ok = snapshot.value['parent_company_ok'],
timestamp = snapshot.value['timestamp'];
}
然而,它在List map = snapshot.value; 的getItems( ) 中失败了。例外:_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<dynamic>'
另外,如果有人可以提供在 List 小部件中显示孩子的代码,我会非常高兴 :)
这是我在 firebase 实时数据库中的数据结构:
【问题讨论】:
标签: firebase firebase-realtime-database flutter