stream - 当应用程序中的主页小部件启动时,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 仅在发现数据已添加时才构建自己到溪流?
解决方案
推荐阅读
- reactjs - 未捕获的错误:Reference.push 失败:第一个参数在属性中包含 NaN
- c++ - 为什么包含标题时出现多个定义错误?
- c++ - 为什么不编译下面的数组的 const 左值引用?
- java - Android studio:Java 代码无法将输入发送到 Captive 门户网站
- angular - 如何在角度中使用带有 td 属性 colspan 的属性绑定?
- reactjs - ReactJS 在 JSON 对象上嵌套循环
- python - 获取超链接的值并单击 python selenium webdriver 中的链接
- javascript - React-table:使用复选框将访问器值保存在状态中
- pandas - 尝试从 Pandas 数据框创建 Seaborn 热图
- php - 如何从 Laravel 应用程序的服务器文件夹中预览 pdf、Word 或图像文件?