Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

currentUser Flutter won't change state

I want two show a widget if the user is logged in and show a different one, if not.

This is what the snippet of the code looks like:

    class _AccountWidgetState extends State<AccountWidget> {
      bool loggedIn = false;
      final _auth = FirebaseAuth.instance;
      late User loggedInUser;
    
      @override
      void initState() {
        getCurrentUser();
        super.initState();
      }
    
      void getCurrentUser() {
        try {
          final user = _auth.currentUser;
          if (user != null) {
            loggedInUser = user;
            setState(() {
              loggedIn == true;
            });
          }
        } catch (e) {
          print(e);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: loggedIn
              ? const Text('logged in')
              : SizedBox(...     ),
    );
  }
}

No matter what the loggedIn boolean will be always false.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What’s going on here?

>Solution :

You are not making any assignment here. It is a condition.

 void getCurrentUser() {
    try {
      final user = _auth.currentUser;
      if (user != null) {
        loggedInUser = user;
        setState(() {
        //  Wrong ->  loggedIn == true; 
        loggedIn = true; 
        });
      }
    } catch (e) {
      print(e);
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading