How to Reload Flutter StatefulWidget with AutomaticKeepAliveClientMixin?
The below code is Not reloading the Usermovies list StreamBuilder on user logout through firebase, instead showing old user movies data only.
This HomeScreen is called in Bottom Navigation Bar with PageView. The other Page is AccountScreen with Login and Logout buttons.
My question is how to reload the UserMovies on user logout through firebase. How to reload the HomeScreen on logout from AccountScreen such that the User Movies Stream is refreshed to null.
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
// need to call super method for AutomaticKeepAliveClientMixin
super.build(context);
print('Rebuild in Home Screen.....');
return StreamBuilder<app.User>(
stream: Provider.of<AuthProvider>(context, listen: true).user,
builder: (context, snapshot) {
if (snapshot.data != null) {
isUserLoggedIn = true;
rebuild = false;
} else if (snapshot.data == null && isUserLoggedIn) {
isUserLoggedIn = false;
rebuild = true;
} else {
isUserLoggedIn = false;
rebuild = false;
}
if (rebuild) {
// Not reloading the Usermovies on user logout, instead showing old user movies data only in the below stream builder
Future.delayed(Duration.zero, () => setState(() {}));
}
return StreamBuilder<List<UserMovies>>(
stream: Provider.of<UserDetailsProvider>(context,
listen: false)
.getUserFavouriteMovies(),
builder: (context, snapshot) {
snapshot.data != null && snapshot.data.length > 0
? print('data there: ')
: print('data zero');
snapshot.data != null && snapshot.data.length > 0
? Scaffold.of(context).showCurrentSnackBar() // to show last favourite movie
: Scaffold.of(context).hideCurrentSnackBar();
return SizedBox(height: 2.0);
},
},
),
}
}