init static method
void
init()
Initializes GUEH.
This method sets up custom error handlers for both Flutter errors and platform dispatcher errors. It also preserves any existing error handlers set by the publisher or other libraries.
Implementation
static void init() {
/// we don't want to initialize Gueh more than once since we are setting global error handlers,
/// calling init multiple times will add multiple _tblFlutterErrorHandler & _tblPlatformDispatcherErrorHandler error handlers which is not desired and can lead to unexpected behavior.
if (_isInitialized) {
return;
}
// hold reference to publisher / other libs error handlers if exist
_publisherFlutterErrorHandler = FlutterError.onError;
_publisherPlatformDispatcherErrorHandler =
PlatformDispatcher.instance.onError;
FlutterError.onError = (details) {
_tblFlutterErrorHandler(details);
// check if publisher / other libs error handler exists and invoke it
if (_publisherFlutterErrorHandler != null) {
try {
_publisherFlutterErrorHandler!(details);
} catch (e, s) {
print("Error invoke _publisherFlutterErrorHandler : $e\n$s");
}
}
FlutterError.presentError(details);
};
PlatformDispatcher.instance.onError = (error, stack) {
try {
// check if publisher / other libs error handler exists and invoke it
if (_publisherPlatformDispatcherErrorHandler != null) {
_publisherPlatformDispatcherErrorHandler!(error, stack);
}
} catch (e, s) {
print("Error invoke _publisherPlatformDispatcherErrorHandler: $e\n$s");
}
return _tblPlatformDispatcherErrorHandler(error, stack);
};
_isInitialized = true;
}