-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,34 @@ | ||
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'; | ||
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/data/onboarding_repository.dart'; | ||
import 'package:starter_architecture_flutter_firebase/src/app_startup.dart'; | ||
// ignore:depend_on_referenced_packages | ||
import 'package:flutter_web_plugins/url_strategy.dart'; | ||
|
||
Future<void> main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
await Firebase.initializeApp( | ||
options: DefaultFirebaseOptions.currentPlatform, | ||
); | ||
// await Firebase.initializeApp( | ||
// options: DefaultFirebaseOptions.currentPlatform, | ||
// ); | ||
// final sharedPreferences = await SharedPreferences.getInstance(); | ||
// 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), | ||
), | ||
], | ||
); | ||
runApp(UncontrolledProviderScope( | ||
container: container, | ||
child: const MyApp(), | ||
runApp(const ProviderScope( | ||
child: AsyncApp(), | ||
)); | ||
} | ||
|
||
void registerErrorHandlers() { | ||
// * Show some error UI if any uncaught exception happens | ||
FlutterError.onError = (FlutterErrorDetails details) { | ||
FlutterError.presentError(details); | ||
debugPrint(details.toString()); | ||
}; | ||
// * Handle errors from the underlying platform/OS | ||
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) { | ||
debugPrint(error.toString()); | ||
return true; | ||
}; | ||
// * Show some error UI when any widget in the app fails to build | ||
ErrorWidget.builder = (FlutterErrorDetails details) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
backgroundColor: Colors.red, | ||
title: Text('An error occurred'.hardcoded), | ||
), | ||
body: Center(child: Text(details.toString())), | ||
); | ||
}; | ||
// final container = ProviderContainer( | ||
// overrides: [ | ||
// onboardingRepositoryProvider.overrideWithValue( | ||
// OnboardingRepository(sharedPreferences), | ||
// ), | ||
// ], | ||
// ); | ||
// runApp(UncontrolledProviderScope( | ||
// container: container, | ||
// child: const MyApp(), | ||
// )); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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:riverpod_annotation/riverpod_annotation.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/features/onboarding/data/onboarding_repository.dart'; | ||
import 'package:starter_architecture_flutter_firebase/src/localization/string_hardcoded.dart'; | ||
|
||
part 'app_startup.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
FutureOr<List<Override>> asyncInit(AsyncInitRef ref) async { | ||
await Firebase.initializeApp( | ||
//name: 'unknown', | ||
options: DefaultFirebaseOptions.currentPlatform, | ||
); | ||
final sharedPreferences = await SharedPreferences.getInstance(); | ||
return [ | ||
onboardingRepositoryProvider.overrideWithValue( | ||
OnboardingRepository(sharedPreferences), | ||
), | ||
]; | ||
} | ||
|
||
class AsyncApp extends ConsumerWidget { | ||
const AsyncApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
// * Get all the overrides | ||
final overridesAsync = ref.watch(asyncInitProvider); | ||
return overridesAsync.when( | ||
data: (List<Override> overrides) { | ||
return ProviderScope( | ||
overrides: overrides, | ||
child: const MainApp(), | ||
); | ||
}, | ||
// * Show an error if initialization failed | ||
error: (Object error, StackTrace stackTrace) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
body: Center(child: Text(error.toString())), | ||
), | ||
); | ||
}, | ||
// * Show some loading UI while the initialization is in progress | ||
loading: () { | ||
return const MaterialApp( | ||
home: Scaffold( | ||
body: Center(child: CircularProgressIndicator()), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
void registerErrorHandlers() { | ||
// * Show some error UI if any uncaught exception happens | ||
FlutterError.onError = (FlutterErrorDetails details) { | ||
FlutterError.presentError(details); | ||
debugPrint(details.toString()); | ||
}; | ||
// * Handle errors from the underlying platform/OS | ||
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) { | ||
debugPrint(error.toString()); | ||
return true; | ||
}; | ||
// * Show some error UI when any widget in the app fails to build | ||
ErrorWidget.builder = (FlutterErrorDetails details) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
backgroundColor: Colors.red, | ||
title: Text('An error occurred'.hardcoded), | ||
), | ||
body: Center(child: Text(details.toString())), | ||
); | ||
}; | ||
} |
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.
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.
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.