Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created September 6, 2023 14:37
Show Gist options
  • Save fredgrott/52a83b8855f248de60ddb9ab3d52177a to your computer and use it in GitHub Desktop.
Save fredgrott/52a83b8855f248de60ddb9ab3d52177a to your computer and use it in GitHub Desktop.
new way to catch flutter app errors
import 'package:flutter/material.dart';
import 'dart:ui';
Future<void> main() async {
await myErrorsHandler.initialize();
FlutterError.onError = (details) {
FlutterError.presentError(details);
myErrorsHandler.onErrorDetails(details);
};
PlatformDispatcher.instance.onError = (error, stack) {
myErrorsHandler.onError(error, stack);
return true;
};
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, widget) {
Widget error = const Text('...rendering error...');
if (widget is Scaffold || widget is Navigator) {
error = Scaffold(body: Center(child: error));
}
ErrorWidget.builder = (errorDetails) => error;
if (widget != null) return widget;
throw ('widget is null');
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment