Firestore只在进行“读取”时返回空数据(null),使用Provider。

huangapple go评论68阅读模式
英文:

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&lt;List&lt;CategoryModel&gt;&gt; getCateogoriesFromFirebase() {
  final categoryCollection =
      fireStoreInstance.collection(&#39;categories&#39;).snapshots();

  return categoryCollection.map((querySnapshots) {
    var categories = &lt;CategoryModel&gt;[];

    for (var singleCate in querySnapshots.docs) {
      CategoryModel categoryModel = CategoryModel.fromFirebase(singleCate);

      categories.add(categoryModel);
    }

    return categories;
  });
}

here is the controller part code

Stream&lt;List&lt;CategoryModel&gt;&gt; getCategoriesFiresotre() {
  return _dataBaseService.getCateogoriesFromFirebase();
}

here is the view code

SizedBox(

  height: 72,
  child: StreamBuilder(
      stream: Provider.of&lt;HomeController&gt;(
        context,
      ).getCategoriesFiresotre(),
      builder: (context, snapsshot) {
        if (snapsshot.data?.length == 0) {
          return Text(&#39;empty&#39;);
        }

        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 ?? &#39;Category&#39;),
                  ),
                );
              });
        }
        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.
Firestore只在进行“读取”时返回空数据(null),使用Provider。

答案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.

huangapple
  • 本文由 发表于 2023年7月3日 15:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定