Skip to content

Commit 8bd2eaa

Browse files
committed
✨ Added bootstrap for running flutter apps
1 parent 95760e2 commit 8bd2eaa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/common/run_app_bootstrap.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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(Future<Widget> Function() builder, {OnError? onError}) async {
12+
final errorLogger = onError ??
13+
(exception, stackTrace) {
14+
BoltLogger.shock([exception, stackTrace]);
15+
};
16+
17+
await runZonedGuarded(
18+
() async {
19+
WidgetsFlutterBinding.ensureInitialized();
20+
FlutterError.onError = (details) => errorLogger(details.exception, details.stack);
21+
22+
runApp(await builder());
23+
},
24+
errorLogger,
25+
);
26+
}

0 commit comments

Comments
 (0)