来自Firestore的数据未在屏幕上显示(扑响)

发布于 2025-02-08 04:43:56 字数 2544 浏览 1 评论 0原文

我试图在流构建器中从Firestore中获取数据(全部在列表视图中)。我现在使用的方法的问题在于屏幕上没有显示任何内容。我尝试设置状态(检查屏幕是否已安装后,但无济于事)。 我知道正在拉出正确的数据是因为它打印到屏幕上,但是由于某种原因,它只是在屏幕上没有显示。

以下是我列表的示例:

Widget _showFriends() {
    return Expanded(
      child: StreamBuilder<List<Future<AppUser>>>(
        stream: db.friendStream(),
          builder: (context, snapshot) {
            if(snapshot.hasData) {
              print("in here");
              final friends = snapshot.data!;
              var friend;
              String username = "";
              String firstName = "";
              String photoUrl = "";

              return ListView.builder(
                keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                   friends[index].then((value) {
                     friend = value;
                     username = value.username;
                     firstName = value.firstName;
                     //photoUrl = value.photoUrl;
                     print("friend username  -- " + username);
                   });
                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context)
                      => IndividualProfile(
                        sourceType: SourceType.friends,
                        name: friend.firstName,
                        userName: friend.username,
                        aboutMe: friend.aboutMe,
                        photoUrl: friend.photoUrl,
                        individualUid: friend.uid,
                      )
                      ));
                    },
                    child: ListTile(
                      title: Text(username),
                      subtitle: Text(firstName),
                      /*leading: CircleAvatar(
                        backgroundImage: CachedNetworkImageProvider(photoUrl),
                      ),*/
                    ),
                  );
                },
              );
            }
            else if(snapshot.hasError) {
              print(snapshot.error.toString());
              return const Center(
                child: Text("An error occurred please try again later"),
              );
            }
            else {
              return const Center(
                child: CircularProgressIndicator(),
              );
            }
          }
      )
    );
  }

请让我知道任何建议。我非常感谢!

I am trying to asynchronously pull data from firestore in a streambuilder (all within a list view). The problem with the approach I have now is that nothing displays on the screen. I tried setting state (after i check if the screen is mounted, but to no avail).
I know the correct data is being pulled because it prints to the screen, but for some reason, it just does not display on the screen.

here's a sample of my list view below:

Widget _showFriends() {
    return Expanded(
      child: StreamBuilder<List<Future<AppUser>>>(
        stream: db.friendStream(),
          builder: (context, snapshot) {
            if(snapshot.hasData) {
              print("in here");
              final friends = snapshot.data!;
              var friend;
              String username = "";
              String firstName = "";
              String photoUrl = "";

              return ListView.builder(
                keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                   friends[index].then((value) {
                     friend = value;
                     username = value.username;
                     firstName = value.firstName;
                     //photoUrl = value.photoUrl;
                     print("friend username  -- " + username);
                   });
                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context)
                      => IndividualProfile(
                        sourceType: SourceType.friends,
                        name: friend.firstName,
                        userName: friend.username,
                        aboutMe: friend.aboutMe,
                        photoUrl: friend.photoUrl,
                        individualUid: friend.uid,
                      )
                      ));
                    },
                    child: ListTile(
                      title: Text(username),
                      subtitle: Text(firstName),
                      /*leading: CircleAvatar(
                        backgroundImage: CachedNetworkImageProvider(photoUrl),
                      ),*/
                    ),
                  );
                },
              );
            }
            else if(snapshot.hasError) {
              print(snapshot.error.toString());
              return const Center(
                child: Text("An error occurred please try again later"),
              );
            }
            else {
              return const Center(
                child: CircularProgressIndicator(),
              );
            }
          }
      )
    );
  }

Pleas let me know any suggestions. I thoroughly appreciate it!

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

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

发布评论

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

评论(1

∞觅青森が 2025-02-15 04:43:56

问题是朋友[index]Future。您没有等待它在返回geduredetector之前。

返回geturetetector(...)将在friends [index] .then回调之前调用。当返回getruestector(...)时,变量friend仍然是null的。

等待的正确方法将与futurebuilder一起使用。

尝试以下操作:

Widget _showFriends() {
  return Expanded(
    child: StreamBuilder<List<Future<AppUser>>>(
      stream: db.friendStream(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          final friends = snapshot.data!;

          return ListView.builder(
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
            itemCount: snapshot.data!.length,
            itemBuilder: (context, index) {
              // here I am awaiting friends[index]'s future
              return FutureBuilder<AppUser>(
                future: friends[index],
                builder:
                    (BuildContext context, AsyncSnapshot<AppUser> snapshot) {
                  if (snapshot.hasError) {
                    return const Center(
                      child: Text(
                        'An error occurred. Please try again later',
                      ),
                    );
                  }
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const Center(child: CircularProgressIndicator());
                  }
                  final friend = snapshot.data!;

                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => IndividualProfile(
                            sourceType: SourceType.friends,
                            name: friend.firstName,
                            userName: friend.username,
                            aboutMe: friend.aboutMe,
                            photoUrl: friend.photoUrl,
                            individualUid: friend.uid,
                          ),
                        ),
                      );
                    },
                    child: ListTile(
                      title: Text(friend.username),
                      subtitle: Text(friend.firstName),
                      // leading: CircleAvatar(
                      //   backgroundImage:
                      //       CachedNetworkImageProvider(friend.photoUrl),
                      // ),
                    ),
                  );
                },
              );
            },
          );
        }
        if (snapshot.hasError) {
          print(snapshot.error.toString());
          return const Center(
            child: Text('An error occurred please try again later'),
          );
        }
        return const Center(child: CircularProgressIndicator());
      },
    ),
  );
}

The problem is that friends[index] is a future. You did not await it before returning GestureDetector.

return GestureDetector(...) would be called before friends[index].then callback. Variable friend would still be null when return GestureDetector(...) is called.

The correct way to wait for it would be with a FutureBuilder.

Try this:

Widget _showFriends() {
  return Expanded(
    child: StreamBuilder<List<Future<AppUser>>>(
      stream: db.friendStream(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          final friends = snapshot.data!;

          return ListView.builder(
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
            itemCount: snapshot.data!.length,
            itemBuilder: (context, index) {
              // here I am awaiting friends[index]'s future
              return FutureBuilder<AppUser>(
                future: friends[index],
                builder:
                    (BuildContext context, AsyncSnapshot<AppUser> snapshot) {
                  if (snapshot.hasError) {
                    return const Center(
                      child: Text(
                        'An error occurred. Please try again later',
                      ),
                    );
                  }
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const Center(child: CircularProgressIndicator());
                  }
                  final friend = snapshot.data!;

                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) => IndividualProfile(
                            sourceType: SourceType.friends,
                            name: friend.firstName,
                            userName: friend.username,
                            aboutMe: friend.aboutMe,
                            photoUrl: friend.photoUrl,
                            individualUid: friend.uid,
                          ),
                        ),
                      );
                    },
                    child: ListTile(
                      title: Text(friend.username),
                      subtitle: Text(friend.firstName),
                      // leading: CircleAvatar(
                      //   backgroundImage:
                      //       CachedNetworkImageProvider(friend.photoUrl),
                      // ),
                    ),
                  );
                },
              );
            },
          );
        }
        if (snapshot.hasError) {
          print(snapshot.error.toString());
          return const Center(
            child: Text('An error occurred please try again later'),
          );
        }
        return const Center(child: CircularProgressIndicator());
      },
    ),
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文