1 | initial version |
You can use the Firestore.instance.collection().document().get()
method to retrieve the value from a document in Firebase. Here's an example code snippet that shows how you can perform this operation and utilize it in a StreamBuilder to perform login navigation:
StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
return StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance.collection('users').document(snapshot.data.uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Text('Loading...');
}
var userData = snapshot.data.data;
if (userData == null) {
return LoginPage();
} else {
return HomePage();
}
}
);
}
}
);
This code snippet first checks if the user is logged in by listening to the FirebaseAuth onAuthStateChanged
stream. If the user is logged in, it then retrieves the user data from the Firestore collection using the Firestore.instance.collection().document().snapshots()
method. It then checks if the retrieved user data is empty or not and navigates to the relevant page accordingly.