Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

State is Object? for Code Generated Notifier

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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();
  }

enter image description here

For more information check this article: https://codewithandrea.com/articles/flutter-riverpod-async-notifier/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading