ark_error_manager_flutter 0.1.0-dev.2
ark_error_manager_flutter: ^0.1.0-dev.2 copied to clipboard
Flutter capture hooks and presentation integration for Ark Error Manager.
example/main.dart
import 'dart:async';
import 'package:ark_error_manager_flutter/ark_error_manager_flutter.dart';
import 'package:flutter/material.dart';
void main() => ErrorExampleBootstrap().run();
final class ErrorExampleBootstrap {
ErrorManager? _manager;
void run() {
runZonedGuarded(_start, _onZoneError);
}
void _start() {
WidgetsFlutterBinding.ensureInitialized();
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
final ErrorManager manager = ErrorManager(
configuration: ErrorManagerConfiguration(
environment: FlutterErrorBuildMode.environment(
deployment: ErrorDeploymentEnvironment.development,
application: const ErrorApplicationInfo(
name: 'Ark Error Manager Flutter example',
version: '0.1.0',
),
),
classifiers: const <ErrorClassifier>[FlutterFrameworkErrorClassifier()],
contextProviders: <ErrorContextProvider>[
FlutterRuntimeErrorContextProvider(),
],
reporters: const <ErrorReporter>[
DeveloperLogErrorReporter(name: 'ark_error_manager_flutter.example'),
],
presenter: NavigatorErrorPresenter(
navigatorKey: navigatorKey,
delegate: const ExampleErrorPresentationDelegate(),
),
),
);
FlutterErrorManagerBinding(manager: manager).attach();
_manager = manager;
runApp(ErrorExampleApplication(navigatorKey: navigatorKey));
}
void _onZoneError(final Object error, final StackTrace stackTrace) {
final ErrorManager? manager = _manager;
if (manager == null) {
Zone.root.handleUncaughtError(error, stackTrace);
return;
}
manager.onUncaughtZoneError(error, stackTrace);
}
}
final class ExampleErrorPresentationDelegate
implements FlutterErrorPresentationDelegate {
const ExampleErrorPresentationDelegate();
@override
Future<void> present(
final BuildContext? context,
final ErrorIncident incident,
final ErrorPresentationDirective directive,
) async {
if (context == null || directive == ErrorPresentationDirective.none) {
return;
}
await showDialog<void>(
context: context,
barrierDismissible: directive != ErrorPresentationDirective.blocking,
builder: (final BuildContext context) => AlertDialog(
title: const Text('Something went wrong'),
content: Text(
'Category: ${incident.assessment.category.name}\n'
'Severity: ${incident.assessment.severity.name}',
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
}
}
final class ErrorExampleApplication extends StatelessWidget {
const ErrorExampleApplication({required this.navigatorKey, super.key});
final GlobalKey<NavigatorState> navigatorKey;
@override
Widget build(final BuildContext context) => MaterialApp(
navigatorKey: navigatorKey,
title: 'Ark Error Manager Flutter example',
home: const ErrorExampleScreen(),
);
}
final class ErrorExampleScreen extends StatelessWidget {
const ErrorExampleScreen({super.key});
@override
Widget build(final BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Error boundaries')),
body: Center(
child: FilledButton(
onPressed: () {
FlutterError.reportError(
FlutterErrorDetails(
exception: StateError('Example framework failure'),
stack: StackTrace.current,
library: 'ark_error_manager_flutter example',
context: ErrorDescription('while handling the example action'),
),
);
},
child: const Text('Report a Flutter error'),
),
),
);
}