I am using riverpod as state management for my flutter project and am currently working on auth services. I am having AuthNotifier extending AsyncNotifier. The problem is when i make a call to the AuthService for example like that:
Future<void> logout() async {
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
return await authService.logout();
});
state.when(
data: (data){},
error: (error, stackTrace){
ref.read(errorProvider.notifier).createException(exception: error.toString(), errorTitle: "Logout Error");
},
loading: (){},
);
}
I am only using the error in the state.when but i also have this unused code.
I tried looking for at the methods of state or state.when but couldn't find anything either because the answer is not there or because i didn't see it. If anyone has alternative on handling the error inside the Notifier i am open to suggestions.
Use whenOrNull
/mapOrNull
or maybeWhen
/maybeMap
to handle only some states, for example:
final Object? newState = state.whenOrNull(
error: (error, stackTrace){
...
},
);
// or
final Object newState = state.whenOrNull(
orElse: () => ...,
error: (error, stackTrace){
...
},
);