//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");
}),
Whenever I use app from start the list of users doesn't appear in the row widget .As
soon as I press hot restart when I am in home widget the list of users in row appears...Is there something wrong with my code that streambuilder is not working? Does streambuilder works only when you add data to the stream or does it also check previous datas and builds? I mean to say when user logs in the app and does nothing, I want all the users to be shown in the row widget just like in messenger app.....does streambuilder only builds itself when it finds that the data has been added to the stream?