daybook_health 0.1.0
daybook_health: ^0.1.0 copied to clipboard
Errors and app health for Flutter on Daybook: Dart errors caught by themselves with readable frames, breadcrumbs, sessions, kept on disk while offline, sent to Daybook's Health hub.
example/lib/main.dart
import 'package:daybook/daybook.dart';
import 'package:daybook_health/daybook_health.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
/// The key and the API come from the command line, so nothing secret or local is committed:
///
/// flutter run --dart-define=DAYBOOK_KEY=dbk_… [--dart-define=DAYBOOK_API=http://…]
const _key = String.fromEnvironment('DAYBOOK_KEY');
const _api = String.fromEnvironment('DAYBOOK_API', defaultValue: Daybook.defaultServerUrl);
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
DaybookHealth.configure(inAppPackages: {'daybook_health_example'});
final daybook = await Daybook.initialize(_key, serverUrl: _api, logging: true);
runApp(ExampleApp(health: daybook.health));
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key, required this.health});
final DaybookHealth health;
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [HealthNavigatorObserver()],
initialRoute: '/',
routes: {
'/': (_) => _Home(health: health),
'/cart': (_) => const _Cart(),
},
);
}
}
class _Home extends StatelessWidget {
const _Home({required this.health});
final DaybookHealth health;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('daybook_health')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Text(_key.isEmpty ? 'No key: run with --dart-define=DAYBOOK_KEY=dbk_…' : 'Sending to $_api'),
const SizedBox(height: 8),
Text('session: ${health.sessionId ?? 'none'}'),
const SizedBox(height: 16),
FilledButton(
// Not caught by the app: Flutter's handler reports it, and Daybook sends it as unhandled.
onPressed: () => throw StateError('The example threw this on purpose'),
child: const Text('Throw an error'),
),
const SizedBox(height: 8),
FilledButton.tonal(
onPressed: () async {
// Not caught either, and asynchronous: the platform dispatcher's handler sends it.
await Future<void>.delayed(const Duration(milliseconds: 10));
throw ArgumentError.value(-1, 'quantity', 'must be positive');
},
child: const Text('Throw after an await'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: () {
try {
int.parse('not a number');
} catch (error, stackTrace) {
health.captureException(error, stackTrace, tags: {'screen': 'home'});
}
},
child: const Text('Catch one and send it'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: () => health.captureMessage('The person opened the example', level: HealthLevel.warning),
child: const Text('Send a message'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: () {
health.addHttpBreadcrumb(method: 'GET', url: Uri.parse('https://example.com/cart?token=secret'), statusCode: 500);
Navigator.of(context).pushNamed('/cart');
},
child: const Text('Open the cart (a breadcrumb)'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: () => health.sendTestError(),
child: const Text('Send a test error'),
),
const SizedBox(height: 16),
// Native crashes (iOS): the app dies, and the crash is sent on the next launch.
for (final (how, label) in const [('unwrap', 'Crash: force unwrap of nil'), ('index', 'Crash: index out of range'), ('nsexception', 'Crash: NSException')])
OutlinedButton(
onPressed: () => const MethodChannel('daybook_health_example/crash').invokeMethod<void>(how),
child: Text(label),
),
const SizedBox(height: 8),
TextButton(
onPressed: () => health.flush(),
child: const Text('Send now'),
),
],
),
);
}
}
class _Cart extends StatelessWidget {
const _Cart();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Cart')),
body: Center(
child: FilledButton(
onPressed: () => throw RangeError.index(3, const [1, 2], 'items'),
child: const Text('Throw on the cart'),
),
),
);
}
}