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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-07 11:00:00 +0000
Seen: 10 times
Last updated: Mar 20 '23
How can I convert a Document in Python?
What is the method for creating a subcollection in Firebase Firestore?
What is the method of using relative position in CSS?
How to search for data in a subcollection of Firestore using a field from the main document?
What is the method to obtain the ids of documents in a list using Flutter Firestore?
What is the method to immediately modify the title of a React page upon loading?
What types of document IDs are available for the documents in the DocuSign API?
Does fetching a document from Firestore also retrieve the collections contained within it?