When I try to use the FutureBuilder Widget to await a boolean value from my Firebase Database, it is not returning the values I want.
The return LoginPage() it's working fine.
Both print1 and print2 are being executed.6
Widget build(BuildContext context) {
return StreamBuilder(
stream: Auth().authStateChanges,
builder: (context, snapshot) {
if (snapshot.hasData) {
return FutureBuilder(
future: _getCurrentUserData(),
builder: ((context, AsyncSnapshot<bool> hasAccountSetup) {
if (hasAccountSetup.data == true) {
print("print1");
return MyHomePage();
} else {
print("print2");
return AccountSetup();
}
}));
} else {
return LoginPage();
}
},
);
}
Future<bool> _getCurrentUserData() async {
final DocumentSnapshot userDoc = await FirebaseFirestore.instance
.collection('Users')
.doc(Auth().currentUser!.uid)
.get();
return userDoc.get('HasSetupAccount');
}
If HasSetupAccount is a field from your firestore database then try this:
Future<bool> _getCurrentUserData() async {
final DocumentSnapshot userDoc = await FirebaseFirestore.instance
.collection('Users')
.doc(Auth().currentUser!.uid)
.get();
Map<String, dynamic> data = docSnapshot.data()!;
//HasSetupAccount should be the name of the field you want to access
return data['HasSetupAccount'];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.