首页 > 解决方案 > 当应用程序中的主页小部件启动时,StreamBuilder 不会构建小部件。需要热重启才能从 firebase 获取所有用户

问题描述

//Inside home.dart file 

Stream<List<Usern>> getAllUsers() async* {
var reference = firestore.collection("users").snapshots();
Usern user;
List<Usern> users = [];

reference.forEach((element) {
  element.docs.forEach((doc) {
    Map data = doc.data();
    user = Usern.fromJSON(data);

    users.add(user);
  });
});

yield users;
  } 
  //Stream Builder code

     StreamBuilder<List<Usern>>(
                stream: getAllUsers(),
                builder: (context, snapshot) {
                  List<Widget> children = [];
                  if (snapshot.hasData) {
                    final List data = snapshot.data;
                    data.forEach((element) {
                      children.add(
                        GestureDetector(
                          onTap: () {
                            print("my id is:$myid");
                            createChatRoom(
                              myid,
                              element.id,
                            );
                          },
                          child: Container(
                              width: 50,
                              height: 50,
                              decoration: BoxDecoration(
                                shape: BoxShape.circle,
                              ),
                              child: Column(
                                children: [
                                  Expanded(
                                    child: CircleAvatar(
                                      radius: 20.0,
                                      backgroundImage: NetworkImage(
                                          element.profilePicture),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 20,
                                  ),
                                  Text(
                                    element.displayName.split(" ")[0],
                                  ),
                                ],
                              )),
                        ),
                      );
                      print(element.displayName);
                    });
                    return Row(
                      children: children,
                    );
                  }
                  return Text("Loading");
                }),

每当我从开始使用应用程序时,用户列表就不会出现在行小部件中。
当我在主页小部件中时按下热重启时,行中的用户列表就会出现......我的代码有问题吗流生成器不工作?streambuilder 是否仅在您将数据添加到流中时才起作用,还是它还检查以前的数据和构建?我的意思是说,当用户登录应用程序并且什么都不做时,我希望所有用户都显示在行小部件中,就像在 messenger 应用程序中一样.....streambuilder 仅在发现数据已添加时才构建自己到溪流?

标签: streamstream-builder

解决方案


推荐阅读