使用时会在firebase扑波

发布于 2025-01-26 08:17:53 字数 1587 浏览 2 评论 0原文

我们正在制作像Instagram这样的主页,我现在只想显示用户帖子。 代码就像这样

 StreamBuilder(
    stream: FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).snapshots(),
    builder: (context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
         return const Center(
             child: CircularProgressIndicator(),
         );
      }
    return PageView.builder(/*the rest of the code ...*/);

起作用,但它显示了所有帖子,所以我添加了此帖子

.where('uid', isEqualTo: uid)

,看起来像这样 内部小部件的内部构建

var uid = FirebaseAuth.instance.currentUser!.uid;

在身体内部

StreamBuilder(
    stream: FirebaseFirestore.instance.collection('posts').where('uid', isEqualTo: uid).orderBy("datePublished", descending: true).snapshots(),
    builder: (context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
         return const Center(
             child: CircularProgressIndicator(),
         );
      }
    return PageView.builder( //to make the page scroll
      itemCount: snapshot.data!.docs.length,/*the rest of the code*/);

,并显示出线上的错误

itemcount:snapshot.data!.docs.length, 说null检查操作员在空值上使用

数据库看起来像

“

we are making home page like Instagram and I want to show only the user post for now.
the code was like this

 StreamBuilder(
    stream: FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).snapshots(),
    builder: (context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
         return const Center(
             child: CircularProgressIndicator(),
         );
      }
    return PageView.builder(/*the rest of the code ...*/);

it worked but it shows all the post so I added this

.where('uid', isEqualTo: uid)

and it looks like this
inside Widget build

var uid = FirebaseAuth.instance.currentUser!.uid;

inside body like this

StreamBuilder(
    stream: FirebaseFirestore.instance.collection('posts').where('uid', isEqualTo: uid).orderBy("datePublished", descending: true).snapshots(),
    builder: (context,
        AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting) {
         return const Center(
             child: CircularProgressIndicator(),
         );
      }
    return PageView.builder( //to make the page scroll
      itemCount: snapshot.data!.docs.length,/*the rest of the code*/);

and it shows an error in line

itemCount: snapshot.data!.docs.length,
saying Null check operator used on a null value

the database look like this

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2025-02-02 08:17:53

该错误表示snapshot.datanull。当snapshot.connectionState等于Connection State.none时,可能会发生这种情况。

根据如何设置浏览量,您可以通过将该行更改为
itemcount:snapshot.data?.docs.length ?? 0,
snapshot.data为null时,它将通过0。

另一种方法是事先检查空值。

if (snapshot.data == null) {
    return SomeWidget():
} else {
    return PageView.builder( //to make the page scroll
      itemCount: snapshot.data!.docs.length,/*the rest of the code*/
    );
}

The error indicates that snapshot.data is null. This could happen when snapshot.connectionState is equal to ConnectionState.none when the stream is initialized and should be protected against.

Depending on how the PageView is set up you could handle this by changing that line to
itemCount: snapshot.data?.docs.length ?? 0,
which will pass 0 when snapshot.data is null.

Another way would be to check for the null value beforehand.

if (snapshot.data == null) {
    return SomeWidget():
} else {
    return PageView.builder( //to make the page scroll
      itemCount: snapshot.data!.docs.length,/*the rest of the code*/
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文