Skip to content

Commit 4b4281e

Browse files
authored
Merge pull request #5 from DutchCodingCompany/feature/run_app
✨ Bootstrap for running flutter apps
2 parents 95760e2 + f232b59 commit 4b4281e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/common/run_app_bootstrap.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'dart:async';
2+
3+
import 'package:dcc_toolkit/logger/bolt_logger.dart';
4+
import 'package:flutter/widgets.dart';
5+
6+
/// A function that handles errors.
7+
typedef OnError = void Function(Object error, StackTrace? stackTrace);
8+
9+
/// Run a Flutter app with a bootstrap that catches errors.
10+
/// By default [onError] will use BoltLogger to log the error.
11+
Future<void> runAppBootstrap(
12+
Future<Widget> Function() builder, {
13+
OnError? onError,
14+
}) async {
15+
final errorLogger = onError ??
16+
(exception, stackTrace) {
17+
BoltLogger.shock([exception, stackTrace]);
18+
};
19+
20+
await runZonedGuarded(
21+
() async {
22+
WidgetsFlutterBinding.ensureInitialized();
23+
FlutterError.onError =
24+
(details) => errorLogger(details.exception, details.stack);
25+
26+
runApp(await builder());
27+
},
28+
errorLogger,
29+
);
30+
}

0 commit comments

Comments
 (0)