Let's say in a Flutter app we want to catch any uncaught exceptions/errors at the top level, so we do this as per the docs:
main() {
// All uncaught errors thrown from synchronous code blocks will end up here:
FlutterError.onError = (FlutterErrorDetails details) {
MyLogger.instance.logUncaughtErrorSync(details);
};
// All uncaught errors thrown from async code blocks will end up here:
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
MyLogger.instance.logUncaughtErrorASync(error, stack);
return true;
};
runApp(const MyApp());
While the docs seems to imply that the distinction between the two is whether the error originated in Dart (and specifically "Flutter") code vs platform (Android/IOS) code, through my testing, the only difference I can tell between the two is whether the Object (error) was thrown from async vs sync Dart code.
For example, if in Dart code, (not platform code), we simply put:
_function() async {
throw('error');
}
and we call that function, it will bubble up through the "platform dispatcher" and is considered a "platform error," but seemingly only because it happened on a background thread?
There appears to actually be no mechanism allowing Exceptions thrown from Java or Swift code to trigger either of these onError functions.
There must be more to it than this though, so whats the point of having two separate mechanisms (with different inputs -- FlutterErrorDetails vs Object+StackTrace)... and what's the real difference?
In fact, the answer is provided in the documentation page you referenced.
FlutterError.onErroris meant for Flutter framework errors - errors that happen at the framework level (errors encountered during the build, layout, and paint phases). In simple words, if you have an error that happens because of your written Dart code or the one in the Flutter framework, it goes toFlutterError.onError.On the other hand,
PlatformDispatcher.instance.onErroris meant for platform-level errors. As you may know, Flutter supports multiple platforms and those execute the code in their native layer. For instance, you use plugins or you implement your own Flutter plugin code using Platform channels:In such case, this is not a Flutter-level error anymore, thus it will be forwarded to
PlatformDispatcher.instance.onError.