I have this FutureBuilder with the future beeing a request for a boolena value from firestore. But when I update the value (hasAccountSetup becames true), nothnig happens until I do a quick reload of the app.
I don't know if I should't be using FutureBuilder in this case, but the goal is for the app to return the HomePage() as soon as I update the hasAccountSetup to true in my firebase db.
Future<bool> _getCurrentUserData() async {
final DocumentSnapshot userDoc = await FirebaseFirestore.instance
.collection('Users')
.doc(Auth().currentUser!.uid)
.get();
return userDoc.get('HasSetupAccount');
}
class _WidgetTreeState extends State<WidgetTree> {
@override
//construtor da class?
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) {
return MyHomePage();
} else {
return AccountSetup();
}
}));
} else {
return LoginPage();
}
},
);
}
You are using StreamBuilder
but the stream is not a stream
it is a future
.
Look at this example of streamBuilder
: Here you can directly listen to changes if any occured in the document without reload
. Tweak this according to your requirement.
class _UserInformationState extends State<UserInformation> {
final _usersStream = FirebaseFirestore.instance
.collection('Users') // 👈 Change the collection name according to your need
.doc(Auth().currentUser!.uid) // 👈 change the document id according to your need
.snapshots();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: _usersStream,
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']);
},
),
),
);
}
}