-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AppStartupWidget class for asynchronous app initialization (#146)
* Add AppStartupWidget class for asynchronous app initialization * Use ref.invalidate inside onRetry
- Loading branch information
Showing
3 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
import 'package:starter_architecture_flutter_firebase/firebase_options.dart'; | ||
import 'package:starter_architecture_flutter_firebase/src/constants/app_sizes.dart'; | ||
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/data/onboarding_repository.dart'; | ||
|
||
part 'app_startup.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
Future<void> appStartup(AppStartupRef ref) async { | ||
// await for all initialization code to be complete before returning | ||
await Future.wait([ | ||
// Firebase init | ||
Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform), | ||
// list of providers to be warmed up | ||
ref.watch(onboardingRepositoryProvider.future) | ||
]); | ||
} | ||
|
||
/// Widget class to manage asynchronous app initialization | ||
class AppStartupWidget extends ConsumerWidget { | ||
const AppStartupWidget({super.key, required this.onLoaded}); | ||
final WidgetBuilder onLoaded; | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final appStartupState = ref.watch(appStartupProvider); | ||
return appStartupState.when( | ||
data: (_) => onLoaded(context), | ||
loading: () => const AppStartupLoadingWidget(), | ||
error: (e, st) => AppStartupErrorWidget( | ||
message: e.toString(), | ||
onRetry: () { | ||
ref.invalidate(onboardingRepositoryProvider); | ||
ref.invalidate(appStartupProvider); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class AppStartupLoadingWidget extends StatelessWidget { | ||
const AppStartupLoadingWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: CircularProgressIndicator(), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class AppStartupErrorWidget extends StatelessWidget { | ||
const AppStartupErrorWidget( | ||
{super.key, required this.message, required this.onRetry}); | ||
final String message; | ||
final VoidCallback onRetry; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Text(message, style: Theme.of(context).textTheme.headlineSmall), | ||
gapH16, | ||
ElevatedButton( | ||
onPressed: onRetry, | ||
child: const Text('Retry'), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.