I have a Riverpod notifier that has a state object called AuthState
. However, when I declare my notifier with the code generation tools it is typing state as object?
instead of AuthState
. Below is the notifier:
@Riverpod(keepAlive: true)
class AuthService extends _$AuthService {
late ApiService _apiService;
late Logger _log;
final _storage = const FlutterSecureStorage();
@override
build() {
_apiService = ref.watch(apiServiceProvider);
_log = ref.watch(loggerProvider);
return AuthState();
}
void testFunc() {
state = state.copyWith(); // doesn't work because state is Object?
}
}
I can see the culprit is inside my auth_service.g.dart file final authServiceProvider = NotifierProvider<AuthService, Object?>...
, but I can't find any way to change Object?
to AuthResult
Declare the return type in your build method so the generator can infer the type:
@override
AuthState build() { ///Here you declare the return type
_apiService = ref.watch(apiServiceProvider);
_log = ref.watch(loggerProvider);
return AuthState();
}
For more information check this article: https://codewithandrea.com/articles/flutter-riverpod-async-notifier/