Skip to content

Commit 299329f

Browse files
Add Firestore+StreamBuilder example
1 parent 9aa78b2 commit 299329f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:flutter/material.dart';
3+
4+
// [START listen_to_realtime_updates_listen_for_updates2]
5+
class UserInformation extends StatefulWidget {
6+
@override
7+
_UserInformationState createState() => _UserInformationState();
8+
}
9+
10+
class _UserInformationState extends State<UserInformation> {
11+
final Stream<QuerySnapshot> _usersStream =
12+
FirebaseFirestore.instance.collection('users').snapshots();
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return StreamBuilder<QuerySnapshot>(
17+
stream: _usersStream,
18+
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
19+
if (snapshot.hasError) {
20+
return const Text('Something went wrong');
21+
}
22+
23+
if (snapshot.connectionState == ConnectionState.waiting) {
24+
return const Text("Loading");
25+
}
26+
27+
return ListView(
28+
children: snapshot.data!.docs
29+
.map((DocumentSnapshot document) {
30+
Map<String, dynamic> data =
31+
document.data()! as Map<String, dynamic>;
32+
return ListTile(
33+
title: Text(data['full_name']),
34+
subtitle: Text(data['company']),
35+
);
36+
})
37+
.toList()
38+
.cast(),
39+
);
40+
},
41+
);
42+
}
43+
}
44+
// [EMD listen_to_realtime_updates_listen_for_updates2]

0 commit comments

Comments
 (0)