英文:
Data is null from Firestore when only Fetching (read), Provider
问题
我正在面对这个问题,结果只在界面上显示“Empty”,我无法找出解决方法。我正在使用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, snapshot) {
if (snapshot.data?.length == 0) {
return Text('empty');
}
if (snapshot.data != null) {
return ListView.builder(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: snapshot.data?.length,
itemBuilder: (c, i) {
return Container(
margin: const EdgeInsets.all(5),
height: 50,
width: 100,
color: Colors.blueGrey,
child: Center(
child: Text(
snapshot.data?[i].category ?? 'Category'),
),
);
});
}
return CircularProgressIndicator();
}),
),
在界面上的输出只有一个Text小部件,内容是"Empty"。我已经检查了Firestore规则,它们都是最新的。
英文:
I am facing the Issue, The result only show's "Empty" in the UI, I can't figure out the solution. I am using Provider as Statemangment. Firebase as backend.
I am only performing a simple action of just fetching the data.
here is the service code
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;
});
}
here is the controller part code
Stream<List<CategoryModel>> getCategoriesFiresotre() {
return _dataBaseService.getCateogoriesFromFirebase();
}
here is the view code
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.
答案1
得分: 1
我的哥哥解决了问题。他能够找出问题所在,问题不在代码中,而是在Firestore
内部。我打错了,写成了categories
而不是categories
。我在末尾多加了一个空格,导致了问题。祝编码开发者好运。
英文:
My older Brother fixed the Problem. He was able to figure out the problem, which wasn't in the code rather than inside Firestore
. I miss typed and wrote categories
instead of categories
. I gave an extra space at the end causing the issue. Good luck Coding devs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论