Skip to content

Commit

Permalink
Use requireValue for providers that need to be initialized asynchrono…
Browse files Browse the repository at this point in the history
…usly (#144)
  • Loading branch information
bizz84 authored Nov 14, 2023
1 parent 9c3d7d8 commit d34ece2
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 120 deletions.
11 changes: 2 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:starter_architecture_flutter_firebase/firebase_options.dart';
import 'package:starter_architecture_flutter_firebase/src/app.dart';
import 'package:starter_architecture_flutter_firebase/src/localization/string_hardcoded.dart';
Expand All @@ -17,19 +16,13 @@ Future<void> main() async {
);
// turn off the # in the URLs on the web
usePathUrlStrategy();
final sharedPreferences = await SharedPreferences.getInstance();
// * Register error handlers. For more info, see:
// * https://docs.flutter.dev/testing/errors
registerErrorHandlers();
// * Entry point of the app

final container = ProviderContainer(
overrides: [
onboardingRepositoryProvider.overrideWithValue(
OnboardingRepository(sharedPreferences),
),
],
);
final container = ProviderContainer();
await container.read(onboardingRepositoryProvider.future);
runApp(UncontrolledProviderScope(
container: container,
child: const MyApp(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/src/features/entries/application/entries_service.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 52 additions & 6 deletions lib/src/features/jobs/data/jobs_repository.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/src/features/onboarding/data/onboarding_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class OnboardingRepository {
}

@Riverpod(keepAlive: true)
OnboardingRepository onboardingRepository(OnboardingRepositoryRef ref) {
throw UnimplementedError();
Future<OnboardingRepository> onboardingRepository(
OnboardingRepositoryRef ref) async {
return OnboardingRepository(await SharedPreferences.getInstance());
}
10 changes: 6 additions & 4 deletions lib/src/features/onboarding/data/onboarding_repository.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class OnboardingController extends _$OnboardingController {
}

Future<void> completeOnboarding() async {
final onboardingRepository = ref.watch(onboardingRepositoryProvider);
final onboardingRepository =
ref.watch(onboardingRepositoryProvider).requireValue;
state = const AsyncLoading();
state = await AsyncValue.guard(onboardingRepository.setOnboardingComplete);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/src/routing/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum AppRoute {
// ignore: unsupported_provider_value
GoRouter goRouter(GoRouterRef ref) {
final authRepository = ref.watch(authRepositoryProvider);
final onboardingRepository = ref.watch(onboardingRepositoryProvider);
final onboardingRepository =
ref.watch(onboardingRepositoryProvider).requireValue;
return GoRouter(
initialLocation: '/signIn',
navigatorKey: _rootNavigatorKey,
Expand Down
5 changes: 3 additions & 2 deletions lib/src/routing/app_router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

1 comment on commit d34ece2

@njwandroid
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bizz84 this is interesting, do you have an article or tip on this?

Please sign in to comment.