I am using a stream builder to detect if user logged in or not.
return StreamBuilder<User>(
stream: AuthService().user,
builder: (context, snapshot) {
if (snapshot.hasData)
return SectionWrapper();
else
return Authentication();
});
and this is the stream I am using
Stream<User> get user {
return _auth.onAuthStateChanged.map(_userFromFirebaseUser);
}
//create user object based on firebase user
User _userFromFirebaseUser(FirebaseUser user) {
return user != null ? User(uid: user.uid, email: user.email) : null;
}
in the section wrapper, there are 2 buttons that navigates to two different sections of the app, when I use sign out method in one of these sections, the stream builder is not updating, and it requires refresh to update the state.
also i tried to put a button in section wrapper to sign out, it works and update UI before I navigate to one of the sections.
and this is the sectionWrapper() widget tree.
Column(
children: <Widget>[
CupertinoButton(
child: Text('Donation & Selling Section'),
onPressed: () {
Navigator.pushReplacementNamed(
context, Section1.routeName,
arguments: user);
}),
CupertinoButton(
child: Text('Bookstores Section'),
onPressed: () {
Navigator.pushReplacementNamed(
context, Section2.routeName);
}),
//works before navigation, does not work after navigation back here
CupertinoButton(
child: Text('Sign out'),
onPressed: () async {
await AuthService().signOut();
}),
],
),
I also tried to use stream provider with consumer and I ended with the same problem.
class Wrapper extends StatelessWidget {
static const String routeName = '/';
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return user == null ? Authentication() : SectionWrapper();
}
}
I wrapped material app with a stream provider.
MultiProvider(
providers: [
StreamProvider<User>.value(value: AuthService().user),
//other providers
],
child: MaterialApp(