starter_architecture_flutter_firebase icon indicating copy to clipboard operation
starter_architecture_flutter_firebase copied to clipboard

AuthRepository that depends on asynchronous providers

Open Veeksi opened this issue 9 months ago • 1 comments

Hey,

Great example and thanks for that!

I am thinking how could I implement the same functionality without using Firebase. My authRepositoryProvider uses dio and hive that are initialized asynchronously in appStartupProvider.

@Riverpod(keepAlive: true)
AuthRepository authRepository(AuthRepositoryRef ref) {
  final dio = ref.watch(dioControllerProvider).requireValue;
  final hiveBox = ref.watch(hiveBoxProvider).requireValue;
  return  AuthRepository(hiveBox, dio);
}

@Riverpod(keepAlive: true)
Future<void> appStartup(AppStartupRef ref) async {
  ref.onDispose(() {
    ref.invalidate(pathControllerProvider);
    ref.invalidate(dioControllerProvider);
    ref.invalidate(hiveBoxProvider);
  });

  await ref.watch(pathControllerProvider.future);
  await ref.watch(dioControllerProvider.future);
  await ref.watch(hiveBoxProvider.future);
}

However my application crashes because those dependencies aren't ready at this point:

@riverpod
GoRouter goRouter(GoRouterRef ref) {
  final appStartupState = ref.watch(appStartupProvider);
  
// THIS LINE BLINKS ERROR BECAUSE DIO AND HIVE ARE STILL BEING INITIALIZED
  final authRepository = ref.watch(authRepositoryProvider); 
  ...
}

Any ideas how could I refactor this so that I can listen authRepository changes like your example does?

Veeksi avatar May 12 '24 12:05 Veeksi