Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AuthRepository that depends on asynchronous providers #153

Open
Veeksi opened this issue May 12, 2024 · 1 comment
Open

AuthRepository that depends on asynchronous providers #153

Veeksi opened this issue May 12, 2024 · 1 comment

Comments

@Veeksi
Copy link

Veeksi commented May 12, 2024

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 Veeksi changed the title AuthRepository that returns Future AuthRepository that depends on asynchronous providers May 12, 2024
@Veeksi
Copy link
Author

Veeksi commented May 12, 2024

For example this kind of solution works but feels like it is not the best one:

@riverpod
GoRouter goRouter(GoRouterRef ref) {
  print("Go router provider");
  final appStartupState = ref.watch(appStartupProvider);

  if (appStartupState.isLoading || appStartupState.hasError) {
    return GoRouter(
      initialLocation: "/startup",
      debugLogDiagnostics: true,
      routes: [
        GoRoute(
          path: '/startup',
          pageBuilder: (context, state) => NoTransitionPage(
            child: AppStartupWidget(
              onLoaded: (_) => const SizedBox.shrink(),
            ),
          ),
        ),
      ],
    );
  }

  final authRepository = ref.watch(authRepositoryProvider);

  return GoRouter(
     ...
  )
}

What do you think @bizz84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant