Created
September 6, 2023 14:37
-
-
Save fredgrott/52a83b8855f248de60ddb9ab3d52177a to your computer and use it in GitHub Desktop.
new way to catch flutter app errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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