GlobalErrorHandler<T extends ErrorData> class
A GlobalErrorHandler to ahndle uncaught errors globally. The handleException function should be used to check the exception and return a
nullable T
that extends the ErrorData object.
This can be used with the riperpod package
Use the handleFlutterError function to handle the flutter errors and handleNonFlutterError to handle the other errors. In your main.dart file Example:
var errorHandler = GlobalErrorHandler(...); // instantiate the error handler
void main() {
runZonedGuarded(() {
FlutterError.onError = (details) {
errorHandler.handleFlutterError(details);
};
runApp(const MyApp()); // entry point into app
}, (error, stack) {
errorHandler.handleNonFlutterError(error, stack);
});
}
Remember to assign the navigationKey to your router to allow for any BuildContext related error displaying.
This class makes use of a new exception ControllerException which can be extended. The Type source is required. Example exception class:
class AppException extends ControllerException {
final Type innerSource;
AppException(this.innerSource);
@override
String get message => 'You are not authorized to access the resources you requested.';
@override
Type get source => innerSource;
@override
String get title => 'App Error';
}
Constructors
-
GlobalErrorHandler.new({required Map<
Type, IAppErrorHandler Function()> controllerHandlers, required Future<void> showErrorMessage(BuildContext context, OverlayState? overlay, T errorDetails), required T? handleException(Object exception), required Widget? riverpodErrorWidget(BuildContext context, T data), }) - Creates a GlobalErrorHandler object.
-
GlobalErrorHandler.withDefaultShowErrorDialog({required Map<
Type, IAppErrorHandler Function()> controllerHandlers, required T? handleException(Object exception), required Widget? riverpodErrorWidget(BuildContext context, T data), }) -
Sets uo a GlobalErrorHandler with a simple alert dialog when errors occur, It is recommend that you use
the regular GlobalErrorHandler constructor for more customization.
factory
Properties
-
controllerHandlers
→ Map<
Type, IAppErrorHandler Function()> -
Represents the collection of controller handlers that will extend IAppErrorHandler in order to
handle the errors globally. Useful when using providers in your application for state management
final
- handleException → T? Function(Object exception)
-
This function parameter accepts an Object exception and converts the data to an ErrorData object,
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
Use this navigator key to enable the showing of any build context related error widgets
getter/setter pair
- riverpodErrorWidget → Widget? Function(BuildContext context, T data)
-
This is useful when initialising async riverpod providers. Should return an error widget when an error occurs during
riverpod provider initialization
related to the available handleInitRiverpodProviderError function.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
showErrorMessage
→ Future<
void> Function(BuildContext context, OverlayState? overlay, T errorDetails) -
Represends the widget (normally an alert dialog) that displays the error when an exeption happens.
One can use an overlay widget to show the error as well.
final
Methods
-
handleFlutterError(
FlutterErrorDetails details) → void - This function should be called to handle flutter errors
-
handleInitRiverpodProviderError(
BuildContext context, Object error, StackTrace trace) → Widget? - Use this function if you're using riverpod and handling errors when initializing the provider.
-
handleNonFlutterError(
Object error, StackTrace trace) → void - This function should be used to handle non flutter errors
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited