I am trying to use runZonedGuarded for the handling of asynchronous error in my Add to App project, but its never goes under the error handling when debugging, only showing as an unhandled exception in logs.
Future<void> main() async {
unawaited(runZonedGuarded(() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (FlutterErrorDetails errorDetails) {
print('This is an error on the Flutter SDK');
print(errorDetails.exception);
print('-----');
print(errorDetails.stack);
};
runApp(app);
}, (error, stackTrace) {
print('Exception handled');
print(error);
print('-----');
print(stackTrace);
}));
Creating the asynchronous exception on button tap as below -
class DebugPage extends StatelessWidget {
DebugPage({
Key key,
this.navigatorKey,
}) : super(key: key);
void createError() async {
print('Creating exception.');
Future.delayed(Duration.zero, () => throw Exception('async error'));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Debug"),
),
body: ListView(
children: <Widget>[
ListTile(title: Text('Test Network Kit....'),
onTap: () {
createError();
}),
],
),
);
}
}
I tried the same process for full flutter app and its working in that project by catching the asynchronous error correctly.
Instead of printing error:
print(stackTrace);
print it :
print(DiagnosticsStackTrace('Stack', stackTrace).getProperties());
So that you'll not be disturbed by flutter, and can handle all error properties.