This manually constructed StateNotifier Provider works in Flutter. However, I cannot figure out how to use code gen to make this same thing. Any ideas?
class SessionProvider extends StateNotifier<Session> {
final Ref ref;
final sessionManager = singleton<ApiClient>().sessionManager;
final client = singleton<ApiClient>().client;
SessionProvider(this.ref, Session initial) : super(initial) {
init();
}
init() {
updateUser();
}
updateUser() async {
final user = sessionManager.signedInUser;
// final profile = await client.profile.me();
state = state.copyWith(
user: user,
);
}
}
Nothing has even come close.
Riverpod code generator won't generate
StateNotifierand its provider. Please note that along with Riverpod 2.0, new classes were introduced:Notifier/AsyncNotifer.StateNotifieris now discouraged in favor of those new APIs.See: From
StateNotifier.The page shows how you can achieve similar thing with
Notifier, to make the migration process fromStateNotifiereasy (see this section). Basically, define astatesetter and anupdatemethod that works similarly to the ones onStateNotifier.So you can define a class like this:
Edit: On a side note, if you're using
StateNotifier, chances are you already use methods to modify the state, so migrating toNotifierwill mostly about changing the way you initialize the state. The above code is more relevant if you're migrating fromStateProvider.