Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - StreamBuilder doesn't update the UI

The StreamBuilder Widget doesn't update the UI ,i still always see the ChoiceScreen which contains two buttons Log In and Sign Up when i click Sign Up it takes me to the SignUpSCreen , pretty same for the LoginScreen , But when i try to Login as a registred User the StreamBuilder doesn't update the Ui and show the ChatScreen widget unless i re-run the application or do a Hot restart to see the updated UI .

Here's what i tried :

StreamBuilder(
            stream: FirebaseAuth.instance.onAuthStateChanged,
            builder: (context, userSnap){
              print('stream builder');
              // a valid token
              if(userSnap.hasData){
                print('valid token .. user logged in');
                return ChatScreen();
              }
              print('not yet');
              return ChoiceScreen();
            },
          ),

The logic seems working , i got :

I/flutter ( 5194): stream builder
I/flutter ( 5194): valid token .. user logged in

But it doesn't return the screen i'm expecting ... and i should do Hot restart to see it ,as i mentioned .

I tried this but the problem persist ..

Any suggestions or advice would be appreciated. Thanks.

like image 201
mhannani Avatar asked Feb 09 '26 21:02

mhannani


1 Answers

I think first you have to initialize a variable. For example,

StreamBuilder(
            stream: FirebaseAuth.instance.onAuthStateChanged,
            builder: (context, userSnap){
              print('stream builder');
              var data = usersSnap.data;
            
              if(data.hasData){
                print('valid token .. user logged in');
                return ChatScreen();
              }
              print('not yet');
              return ChoiceScreen();
            },
          ),
like image 192
Mayur Agarwal Avatar answered Feb 12 '26 16:02

Mayur Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!