Skip to content

Commit

Permalink
fix: showing snackbar results in to empty scaffold error
Browse files Browse the repository at this point in the history
- This fixes the error by wrapping ErrorWidget with a Scaffold
- Also provides providing custom ErrorWidget via flutterErrorBuilder
  • Loading branch information
adar2378 committed Jan 2, 2024
1 parent a94c4f3 commit f1ab4a9
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions packages/djangoflow_app/lib/src/utils/djangoflow_app_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ typedef OnExceptionCallBack = Function(
typedef RootWidgetBuilder = FutureOr<Widget> Function(
AppBuilder Function(AppBuilder appBuilder) builder);

typedef FlutterErrorBuilder = Widget Function(FlutterErrorDetails error);

class DjangoflowAppRunner {
static Future<void> run({
required OnExceptionCallBack onException,
required RootWidgetBuilder rootWidgetBuilder,

/// To provide custom widget for flutter errors
/// This will override the default [ErrorWidget]
/// https://docs.flutter.dev/testing/errors#handling-all-types-of-errors
FlutterErrorBuilder? flutterErrorBuilder,
}) async =>
runZonedGuarded(
() async {
Expand All @@ -30,13 +37,17 @@ class DjangoflowAppRunner {
onException(details.exception, details.stack);
};

if (!kDebugMode) {
ErrorWidget.builder = (FlutterErrorDetails error) {
ErrorWidget.builder = (FlutterErrorDetails error) {
if (error.stack != null) {
Zone.current.handleUncaughtError(error.exception, error.stack!);
}

return _buildDebugErrorWidget(
error: error,
flutterErrorBuilder: flutterErrorBuilder,
);
};

return const Offstage();
};
}
final storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
Expand All @@ -56,4 +67,30 @@ class DjangoflowAppRunner {
onException(exception, stackTrace);
},
);

static Widget _buildDebugErrorWidget({
required FlutterErrorDetails error,
FlutterErrorBuilder? flutterErrorBuilder,
}) {
final exception = error.exception;
String message = '';

try {
message = '${exception.toString()}\n${error.stack.toString()}';
} catch (e) {
message = 'An error was thrown';
}
// Without Scaffold showing snackbar will show error as there are no Scaffold
// to show snackbar
return Scaffold(
body: flutterErrorBuilder != null
? flutterErrorBuilder(error)
: !kDebugMode
? const Offstage()
: ErrorWidget.withDetails(
message: message,
error: exception is FlutterError ? exception : null,
),
);
}
}

0 comments on commit f1ab4a9

Please sign in to comment.