我正在尝试使用flutter和futurebuilder从firebase获取数据
这是代码
FutureBuilder<DocumentSnapshot>(
future: _services.category.doc(_provider.selectedCategory).get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
if(snapshot.hasError){
return Text('something went wrong...');
}
if(snapshot.connectionState == ConnectionState.waiting){
return Center(child: CircularProgressIndicator(),);
}
Map<String, dynamic>data = snapshot.data.data();
return Expanded(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10),
child: Container(
child: Row(
children: [
Text('Main Category: '),
FittedBox(
child: Text(_provider.selectedCategory,
style: TextStyle(fontWeight: FontWeight.bold),)),
],
),
),
),
Divider(thickness: 3,),
Container(
child: Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: ListView.builder(
itemCount: data['subcategory'] == null ? 0: data['subcategory'].length,
itemBuilder: (BuildContext context, int index){
return ListTile(
contentPadding: EdgeInsets.zero,
leading: CircleAvatar(
child: Text('${index + 1}'),
),
title: Text(data['subcategory'][index]['name']),
onTap: (){
_provider.selectSubCategory(data['subcategory'][index]['name']);
Navigator.pop(context);
},
_provider.selectedCategory 来自另一个流构建器,其中用户根据同样来自 firebase 数据的选项选择类别
_services = CollectionReference category = FirebaseFirestore.instance.collection('categories');
但是好像文档快照是空的
谁能帮我解决这个问题?? 谢谢
答案 0 :(得分:1)
尝试添加 if (snapshot.connectionState == ConnectionState.done&& snapshot.hasData && snapshot.data != null){return your widget; }