2

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);
             },
         },
      ),
   }
}
Ralemos
  • 5,571
  • 2
  • 9
  • 18
Ramesh Kumar
  • 147
  • 14

1 Answers1

0

return a check on whether the user exists or not

@override
bool get wantKeepAlive => isUserLoggedIn;;

in the same class listen for your user stream, and keep track of whether the user present or not and set isUserLoggedIn based on that, now state will be maintained if the user exists otherwise not.

initState(){
 Provider.of<AuthProvider>(context, listen: true).user.listen((user){
  isUserLoggedIn = user!=null;
 });
}

here wantKeepAlive is a framework getter method, which is used by flutter framework (the mixin) to decide whether the state must be maintained or not, you can return a boolean which can be dynamic depending on your needs.

Yadu
  • 2,979
  • 2
  • 12
  • 27