Skip to content

Commit

Permalink
Add errorBuilder route to NotFoundScreen (#149)
Browse files Browse the repository at this point in the history
* Add errorBuilder route to NotFoundScreen

* Add rewrites rule to `firebase.json`
  • Loading branch information
bizz84 authored Mar 14, 2024
1 parent 6ad66f3 commit a756a90
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 129 deletions.
8 changes: 7 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"firebase.json",
"**/.*",
"**/node_modules/**"
]
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
5 changes: 5 additions & 0 deletions lib/src/app_startup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ part 'app_startup.g.dart';

@Riverpod(keepAlive: true)
Future<void> appStartup(AppStartupRef ref) async {
ref.listen(onboardingRepositoryProvider, (previous, current) {
if (current.hasError) {
// keep track of error so the provider can be rebuilt on retry
}
});
ref.onDispose(() {
// ensure dependent providers are disposed as well
ref.invalidate(onboardingRepositoryProvider);
Expand Down
43 changes: 43 additions & 0 deletions lib/src/common_widgets/empty_placeholder_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:starter_architecture_flutter_firebase/src/common_widgets/primary_button.dart';
import 'package:starter_architecture_flutter_firebase/src/constants/app_sizes.dart';
import 'package:starter_architecture_flutter_firebase/src/features/authentication/data/firebase_auth_repository.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/app_router.dart';

/// Placeholder widget showing a message and CTA to go back to the home screen.
class EmptyPlaceholderWidget extends ConsumerWidget {
const EmptyPlaceholderWidget({super.key, required this.message});
final String message;

@override
Widget build(BuildContext context, WidgetRef ref) {
return Padding(
padding: const EdgeInsets.all(Sizes.p16),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
message,
style: Theme.of(context).textTheme.headlineMedium,
textAlign: TextAlign.center,
),
gapH32,
PrimaryButton(
onPressed: () {
final isLoggedIn =
ref.watch(authRepositoryProvider).currentUser != null;
context.goNamed(
isLoggedIn ? AppRoute.jobs.name : AppRoute.signIn.name);
},
text: 'Go Home',
)
],
),
),
);
}
}
3 changes: 2 additions & 1 deletion lib/src/routing/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:starter_architecture_flutter_firebase/src/features/jobs/presenta
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/data/onboarding_repository.dart';
import 'package:starter_architecture_flutter_firebase/src/features/onboarding/presentation/onboarding_screen.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/go_router_refresh_stream.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/not_found_screen.dart';
import 'package:starter_architecture_flutter_firebase/src/routing/scaffold_with_nested_navigation.dart';

part 'app_router.g.dart';
Expand Down Expand Up @@ -201,6 +202,6 @@ GoRouter goRouter(GoRouterRef ref) {
],
),
],
//errorBuilder: (context, state) => const NotFoundScreen(),
errorBuilder: (context, state) => const NotFoundScreen(),
);
}
31 changes: 15 additions & 16 deletions lib/src/routing/not_found_screen.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// import 'package:ecommerce_app/src/localization/string_hardcoded.dart';
// import 'package:ecommerce_app/src/common_widgets/empty_placeholder_widget.dart';
// import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:starter_architecture_flutter_firebase/src/common_widgets/empty_placeholder_widget.dart';

// /// Simple not found screen used for 404 errors (page not found on web)
// class NotFoundScreen extends StatelessWidget {
// const NotFoundScreen({super.key});
/// Simple not found screen used for 404 errors (page not found on web)
class NotFoundScreen extends StatelessWidget {
const NotFoundScreen({super.key});

// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(),
// body: EmptyPlaceholderWidget(
// message: '404 - Page not found!'.hardcoded,
// ),
// );
// }
// }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const EmptyPlaceholderWidget(
message: '404 - Page not found!',
),
);
}
}
Loading

0 comments on commit a756a90

Please sign in to comment.