Skip to content

Commit

Permalink
Attempt to add updated initialization code
Browse files Browse the repository at this point in the history
  • Loading branch information
bizz84 committed Oct 16, 2023
1 parent a413c84 commit fa69976
Show file tree
Hide file tree
Showing 18 changed files with 371 additions and 289 deletions.
16 changes: 8 additions & 8 deletions .metadata
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
# This file should be version controlled and should not be manually edited.

version:
revision: 84a1e904f44f9b0e9c4510138010edcc653163f8
channel: stable
revision: "2f708eb8396e362e280fac22cf171c2cb467343c"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 84a1e904f44f9b0e9c4510138010edcc653163f8
base_revision: 84a1e904f44f9b0e9c4510138010edcc653163f8
- platform: macos
create_revision: 84a1e904f44f9b0e9c4510138010edcc653163f8
base_revision: 84a1e904f44f9b0e9c4510138010edcc653163f8
create_revision: 2f708eb8396e362e280fac22cf171c2cb467343c
base_revision: 2f708eb8396e362e280fac22cf171c2cb467343c
- platform: android
create_revision: 2f708eb8396e362e280fac22cf171c2cb467343c
base_revision: 2f708eb8396e362e280fac22cf171c2cb467343c

# User provided section

Expand Down
62 changes: 18 additions & 44 deletions lib/main.dart
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(),
// ));
}
4 changes: 2 additions & 2 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/app_router.dart';

class MyApp extends ConsumerWidget {
const MyApp({super.key});
class MainApp extends ConsumerWidget {
const MainApp({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down
83 changes: 83 additions & 0 deletions lib/src/app_startup.dart
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())),
);
};
}
24 changes: 24 additions & 0 deletions lib/src/app_startup.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.

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.

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.

Loading

0 comments on commit fa69976

Please sign in to comment.