FilledStacks / flutter-tutorials

The repo contains the source code for all the tutorials on the FilledStacks Youtube channel.
MIT License
4.75k stars 1.76k forks source link

ViewModel ( onModelReady ) not trigger after Logout #96

Closed azazadev closed 4 years ago

azazadev commented 4 years ago

I'm using Firebase Authentication Tutorial to Loging with Email/Password and I have implement the Logout method in AuthenticationService

  Future signOut() async {
    try {
      if (_currentUser != null) {
        await _firebaseAuth.signOut();
        _currentUser = null;
      }
    } catch (e) {
      print('Failed to signOut' + e.toString());
    }
  }

so the scenario is :

  1. User_1 Login with email_1/passwod_1 and go to HomeView and in HomeView the onModelReady is trigger for HomeViewModel

    onModelReady: (model) => model.listenToPosts() -> data of user_1 is loaded From FireStore

  2. User_1 Logout -> Trigger signOut method and navigate To WelcomePage

  3. in WelcomePage I'm trying to Login with User_2 email_2/passwod_2

Expected :

onModelReady is trigger in HomeView and the data of user_2 is loaded From FireStore

Observed :

onModelReady is not Triggered in HomeView and the data of user_1 is loaded From FireStore

when I close and open again the app the data of user_2 is Loaded

Question : How I can clean user_1 data after Logout or how I can force all ViewModel to be rebuild again after Logout

FilledStacks commented 4 years ago

When you navigate away from the HomeView is should dispose everything. When you come back it should be a new viewmodel that gets built and onModeReady should trigger. Make sure you're not disabling dispose, or telling stacked to only call onViewmodelReady once.

azazadev commented 4 years ago

thanks for quick answer, I'm using Bottom Navigation Bar and based on wiki we should set the disposeViewModel as false and register ViewModel as singleton to avoid call to Firestore every time we switch between views :

my current Views have this configuration :

    return ViewModelBuilder<HomeViewModel>.reactive(
      disposeViewModel: false,
      fireOnModelReadyOnce: true,
      viewModelBuilder: () => locator<HomeViewModel>(),
      onModelReady: (model) => model.listenToPosts(),

I have test multiple options but no success :(

azazadev commented 4 years ago

I have found this solution :

  1. use flutter_phoenix to force restart application from scratch
void main() {
  // Register all the models and services before the app starts
  setupLocator();

  runApp(
    /// Wrap your App widget in the Phoenix widget
    Phoenix(
      child: MyApp(),
    ),
  );
}
  1. when press Logout Button reset and setup again locator
onPressed: () async {
                      await model.signOut();
                      Phoenix.rebirth(context);
                      model.navigateToWelcomePage();
                      locator.reset();
                      setupLocator();
                    }

and its work now :) , please let me know if you agree with this that I can close issue

Thanks again for help