Skip to content

Commit 3615f9d

Browse files
authored
Update app startup logic to be compatible with URL-based navigation and deep links (#158)
* Start updating the app startup logic * Remove the /startup route (that was silly)
1 parent 2fdc5e7 commit 3615f9d

File tree

4 files changed

+83
-84
lines changed

4 files changed

+83
-84
lines changed

lib/src/app.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_riverpod/flutter_riverpod.dart';
33
import 'package:starter_architecture_flutter_firebase/src/routing/app_router.dart';
4+
import 'package:starter_architecture_flutter_firebase/src/routing/app_startup.dart';
45

56
class MyApp extends ConsumerWidget {
67
const MyApp({super.key});
@@ -12,6 +13,11 @@ class MyApp extends ConsumerWidget {
1213
final goRouter = ref.watch(goRouterProvider);
1314
return MaterialApp.router(
1415
routerConfig: goRouter,
16+
builder: (_, child) {
17+
return AppStartupWidget(
18+
onLoaded: (_) => child!,
19+
);
20+
},
1521
theme: ThemeData(
1622
colorSchemeSeed: primaryColor,
1723
unselectedWidgetColor: Colors.grey,

lib/src/routing/app_router.dart

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:riverpod/riverpod.dart';
33
import 'package:riverpod_annotation/riverpod_annotation.dart';
4-
import 'package:starter_architecture_flutter_firebase/src/routing/app_startup.dart';
54
import 'package:starter_architecture_flutter_firebase/src/features/authentication/data/firebase_auth_repository.dart';
65
import 'package:starter_architecture_flutter_firebase/src/features/authentication/presentation/custom_profile_screen.dart';
76
import 'package:starter_architecture_flutter_firebase/src/features/authentication/presentation/custom_sign_in_screen.dart';
@@ -43,18 +42,12 @@ enum AppRoute {
4342

4443
@riverpod
4544
GoRouter goRouter(Ref ref) {
46-
// rebuild GoRouter when app startup state changes
47-
final appStartupState = ref.watch(appStartupProvider);
4845
final authRepository = ref.watch(authRepositoryProvider);
4946
return GoRouter(
5047
initialLocation: '/signIn',
5148
navigatorKey: _rootNavigatorKey,
5249
debugLogDiagnostics: true,
5350
redirect: (context, state) {
54-
// If the app is still initializing, show the /startup route
55-
if (appStartupState.isLoading || appStartupState.hasError) {
56-
return '/startup';
57-
}
5851
final onboardingRepository =
5952
ref.read(onboardingRepositoryProvider).requireValue;
6053
final didCompleteOnboarding = onboardingRepository.isOnboardingComplete();
@@ -69,14 +62,11 @@ GoRouter goRouter(Ref ref) {
6962
}
7063
final isLoggedIn = authRepository.currentUser != null;
7164
if (isLoggedIn) {
72-
if (path.startsWith('/startup') ||
73-
path.startsWith('/onboarding') ||
74-
path.startsWith('/signIn')) {
65+
if (path.startsWith('/onboarding') || path.startsWith('/signIn')) {
7566
return '/jobs';
7667
}
7768
} else {
78-
if (path.startsWith('/startup') ||
79-
path.startsWith('/onboarding') ||
69+
if (path.startsWith('/onboarding') ||
8070
path.startsWith('/jobs') ||
8171
path.startsWith('/entries') ||
8272
path.startsWith('/account')) {
@@ -87,16 +77,6 @@ GoRouter goRouter(Ref ref) {
8777
},
8878
refreshListenable: GoRouterRefreshStream(authRepository.authStateChanges()),
8979
routes: [
90-
GoRoute(
91-
path: '/startup',
92-
pageBuilder: (context, state) => NoTransitionPage(
93-
child: AppStartupWidget(
94-
// * This is just a placeholder
95-
// * The loaded route will be managed by GoRouter on state change
96-
onLoaded: (_) => const SizedBox.shrink(),
97-
),
98-
),
99-
),
10080
GoRoute(
10181
path: '/onboarding',
10282
name: AppRoute.onboarding.name,

lib/src/routing/app_startup.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Future<void> appStartup(Ref ref) async {
1313
// ensure dependent providers are disposed as well
1414
ref.invalidate(onboardingRepositoryProvider);
1515
});
16+
// Uncomment this to test that URL-based navigation and deep linking works
17+
// even when there's a delay in the app startup logic
18+
// await Future.delayed(Duration(seconds: 1));
1619
// await for all initialization code to be complete before returning
1720
await ref.watch(onboardingRepositoryProvider.future);
1821
}
@@ -76,3 +79,13 @@ class AppStartupErrorWidget extends StatelessWidget {
7679
);
7780
}
7881
}
82+
83+
class AppStartupDataWidget extends StatelessWidget {
84+
const AppStartupDataWidget({super.key, required this.child});
85+
final Widget child;
86+
87+
@override
88+
Widget build(BuildContext context) {
89+
return child;
90+
}
91+
}

0 commit comments

Comments
 (0)