1

Suppose there is a login system. So after login, I need to go to the home screen. After logout, its came to the login screen. So in that case, after logout needs to clear all provider data, listener, state that's related to the home screen, and others.

Also need a clear concept of how to call onDispose() method of a StateNotifierProvider. How to access this from outside of a provider, in case of logout.

1 Answers1

4

The solution would be to have your providers that need to be reset to "watch" the other provider that keeps informations about login

For example:

final authProvider = SomeProvider<User>(<anything>);

final providerThatResetsWhenLogOutOrLogIn = Provider((ref) {
  ref.watch(authProvider);
})
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • 2
    Pls can you be more detailed? – THEODORE Aug 12 '22 at 10:42
  • 1
    @Rémi so you wouldn't want to call invalidate? And what about AsyncNotifiers where they fetch data from an API on build(), how do you prevent them from triggering when a user logs out? – FooBar Nov 09 '22 at 07:40
  • It seems to work but throws this error: ```'package:riverpod/src/async_notifier/base.dart': Failed assertion: line 352 pos 18: '_lastFuture == null': bad state``` – NullByte08 Mar 05 '23 at 04:54