Skip to content

AuthRepository that depends on asynchronous providers #153

@Veeksi

Description

@Veeksi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions