How does @riverpod work? I got The argument type 'AutoDisposeNotifierProviderImpl' error

293 Views Asked by At

I am using riverpod annotation and stuck here.

@riverpod
ShopRepository shopRepository(ShopRepositoryRef ref) => ShopRepositoryImpl(dio: authDio(), managerLocalStorage: ref.watch(managerLocalStorageImplProvider));

managerLocalStorageImplProvider part says,

The argument type 'AutoDisposeNotifierProviderImpl<ManagerLocalStorageImpl, ManagerLocalStorageState>' can't be assigned to the parameter type 'ProviderListenable<ManagerLocalStorage>'. 

Here'are other codes:

// This class doesn't have riverpod annotation.
class ShopRepositoryImpl extends ShopRepository {
  ShopRepositoryImpl({required Dio dio, required ManagerLocalStorage managerLocalStorage}) {
    _apiClient = ShopApiClient(dio, baseUrl: Constants.shopDomain);
    _localStorage = managerLocalStorage;
  }

  late final ShopApiClient _apiClient;
  late final ManagerLocalStorage _localStorage;
}
@riverpod
class ManagerLocalStorageImpl extends _$ManagerLocalStorageImpl implements ManagerLocalStorage {
  late final FlutterSecureStorage _storage;

  @override
  ManagerLocalStorageState build() {
    Log(usePretty: false).d("printed!! build build build");
    _storage = ref.read(secureStorageProvider);
    return ManagerLocalStorageState();
  }
  ...
}

How can I fix this issue?

What I got to know is the build()'s return Type will be the second type of generated Provider's generic. And the generated provider from managerLocalStorageImpl is AutoDisposeNotifierProviderImpl but the argument expects ProviderListenable and the generic has only one type. I don't know even it's possible to pass like this or not.

I am trying to do MVVM pattern. So, it would be appreciated if you can suggest some structured code snippets.

Riverpod seems it is global and can be access from everywhere like against to SOLID. Is there any way to prevent it?

1

There are 1 best solutions below

2
On

AutoDisposeNotifierProviderImpl doesn't conflict with ProviderListenable

ref.watch(managerLocalStorageImplProvider) has type ManagerLocalStorageState, which is not a subclass of ManagerLocalStorage.

if you write like this, dart analyzer would recognize the error

ShopRepository shopRepository(ShopRepositoryRef ref) {
  final managerLocalStorage = ref.watch(managerLocalStorageImplProvider);
  return ShopRepositoryImpl(
      dio: authDio(), managerLocalStorage: managerLocalStorage);
}