There are 2 ways:
1. StreamBuilder
When you want to listen to changes constantly, and want the data to get updated without hot reload/restart
2. FutureBuilder
When you want to get the document only once and have no requirement of listening constantly to the change of the document.
StreamBuilder
1. Multiple Documents
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: StreamBuilder<QuerySnapshot>( stream: FirebaseFirestore .instance. .collection('users') // π Your desired collection name here .snapshots(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.hasError) { return const Text('Something went wrong'); } if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Loading"); } return ListView( children: snapshot.data!.docs.map((DocumentSnapshot document) { Map<String, dynamic> data = document.data()! as Map<String, dynamic>; return ListTile( title: Text(data['fullName']), // π Your valid data here ); }).toList()); }, ), ), ); }
2. Single Document
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>( stream: FirebaseFirestore .instance .collection('users') .doc(FirebaseAuth.instance.currentUser!.uid) // π Your document id change accordingly .snapshots(), builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) { if (snapshot.hasError) { return const Text('Something went wrong'); } if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Loading"); } Map<String, dynamic> data = snapshot.data!.data()! as Map<String, dynamic>; return Text(data['fullName']); // π your valid data here }, ), ), ); }
FutureBuilder
1. Multiple Documents
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: FutureBuilder<QuerySnapshot>( future: FirebaseFirestore .instance .collection('users') // π Your collection name here .get(), builder: (_, snapshot) { if (snapshot.hasError) return Text('Error = ${snapshot.error}'); if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Loading"); } return ListView( children: snapshot.data!.docs.map((DocumentSnapshot document) { Map<String, dynamic> data = document.data()! as Map<String, dynamic>; return ListTile( title: Text(data['avatar']), // π Your valid data here ); }).toList()); }, )), ); }
2. Single Document
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>( future: FirebaseFirestore.instance .collection('users') .doc(FirebaseAuth.instance.currentUser!.uid) // π Your document id change accordingly .get(), builder: (_, snapshot) { if (snapshot.hasError) return Text('Error = ${snapshot.error}'); if (snapshot.connectionState == ConnectionState.waiting) { return const Text("Loading"); } Map<String, dynamic> data = snapshot.data!.data()!; return Text(data['fullName']); //π Your valid data here }, )), ); }