我面对这个问题,结果只在UI中显示为"空",我想不出解决的办法.我正在使用Provider 作为状态管理.作为后端的Firebase.
我只是在执行一个简单的操作,就是获取数据.
这是服务代码
Stream<List<CategoryModel>> getCateogoriesFromFirebase() {
final categoryCollection =
fireStoreInstance.collection('categories').snapshots();
return categoryCollection.map((querySnapshots) {
var categories = <CategoryModel>[];
for (var singleCate in querySnapshots.docs) {
CategoryModel categoryModel = CategoryModel.fromFirebase(singleCate);
categories.add(categoryModel);
}
return categories;
});
}
以下是控制器部分的代码
Stream<List<CategoryModel>> getCategoriesFiresotre() {
return _dataBaseService.getCateogoriesFromFirebase();
}
以下是视图代码
SizedBox(
height: 72,
child: StreamBuilder(
stream: Provider.of<HomeController>(
context,
).getCategoriesFiresotre(),
builder: (context, snapsshot) {
if (snapsshot.data?.length == 0) {
return Text('empty');
}
if (snapsshot.data != null) {
return ListView.builder(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: snapsshot.data?.length,
itemBuilder: (c, i) {
return Container(
margin: const EdgeInsets.all(5),
height: 50,
width: 100,
color: Colors.blueGrey,
child: Center(
child: Text(
snapsshot.data?[i].category ?? 'Category'),
),
);
});
}
return CircularProgressIndicator();
}),
),
The output, In the UI is only the Text
widget "Empty"
I have checked the Firestore rules, They are all up to date.