1

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();
        }
      },
    );
  }
  • what causing this behavior is that you're using a `FutureBuilder` inside `StreamBuilder` which relies on `authStateChanges` which gets triggered only when user logs in, sign up log out..., so until the action is related to a user authentication, the `StreamBuilder` will not rebuild, so the `FutureBuilder` will not rebuild as well until you make a manual state update like a hot-reload – Gwhyyy Jan 17 '23 at 14:55

1 Answers1

1

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']);
          },
        ),
      ),
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • @JaimeFerreira , I would request you to please consider copying the exact code and change the collection name and doc name , according to your requirement and then try, because it is working perfectly fine , I tested it right now, See my updated post – krishnaacharyaa Jan 17 '23 at 15:47
  • Stream is empty because of incorrect collection name and doc ref, try changing it, everything will work very smooth. Please see my pinned lines in the post – krishnaacharyaa Jan 17 '23 at 15:49
  • Directly copy paste my code, I have done the changes for you !, Hope it is helpful – krishnaacharyaa Jan 17 '23 at 16:18