text
stringlengths 1
474
|
|---|
if (widget != null) return widget;
|
throw StateError('widget is null');
|
},
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
Errors not caught by Flutter
|
Consider an onPressed callback that invokes an asynchronous function,
|
such as MethodChannel.invokeMethod (or pretty much any plugin).
|
For example:
|
<code_start>OutlinedButton(
|
child: const Text('Click me!'),
|
onPressed: () async {
|
const channel = MethodChannel('crashy-custom-channel');
|
await channel.invokeMethod('blah');
|
},
|
)<code_end>
|
If invokeMethod throws an error, it won’t be forwarded to FlutterError.onError.
|
Instead, it’s forwarded to the PlatformDispatcher.To catch such an error, use PlatformDispatcher.instance.onError.
|
<code_start>import 'package:flutter/material.dart';
|
import 'dart:ui';
|
void main() {
|
MyBackend myBackend = MyBackend();
|
PlatformDispatcher.instance.onError = (error, stack) {
|
myBackend.sendError(error, stack);
|
return true;
|
};
|
runApp(const MyApp());
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
Handling all types of errors
|
Say you want to exit application on any exception and to display
|
a custom error widget whenever a widget building fails - you can base
|
your errors handling on next code snippet:
|
<code_start>import 'package:flutter/material.dart';
|
import 'dart:ui';
|
Future<void> main() async {
|
await myErrorsHandler.initialize();
|
FlutterError.onError = (details) {
|
FlutterError.presentError(details);
|
myErrorsHandler.onErrorDetails(details);
|
};
|
PlatformDispatcher.instance.onError = (error, stack) {
|
myErrorsHandler.onError(error, stack);
|
return true;
|
};
|
runApp(const MyApp());
|
}
|
class MyApp extends StatelessWidget {
|
const MyApp({super.key});
|
@override
|
Widget build(BuildContext context) {
|
return MaterialApp(
|
builder: (context, widget) {
|
Widget error = const Text('...rendering error...');
|
if (widget is Scaffold || widget is Navigator) {
|
error = Scaffold(body: Center(child: error));
|
}
|
ErrorWidget.builder = (errorDetails) => error;
|
if (widget != null) return widget;
|
throw StateError('widget is null');
|
},
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>Report errors to a service
|
While one always tries to create apps that are free of bugs,
|
they’re sure to crop up from time to time.
|
Since buggy apps lead to unhappy users and customers,
|
it’s important to understand how often your users
|
experience bugs and where those bugs occur.
|
That way, you can prioritize the bugs with the
|
highest impact and work to fix them.How can you determine how often your users experiences bugs?
|
Whenever an error occurs, create a report containing the
|
error that occurred and the associated stacktrace.
|
You can then send the report to an error tracking
|
service, such as Bugsnag, Datadog,
|
Firebase Crashlytics, Rollbar, or Sentry.The error tracking service aggregates all of the crashes your users
|
experience and groups them together. This allows you to know how often your
|
app fails and where the users run into trouble.In this recipe, learn how to report errors to the
|
Sentry crash reporting service using
|
the following steps:<topic_end>
|
<topic_start>
|
1. Get a DSN from Sentry
|
Before reporting errors to Sentry, you need a “DSN” to uniquely identify
|
your app with the Sentry.io service.To get a DSN, use the following steps:<topic_end>
|
<topic_start>
|
2. Import the Sentry package
|
Import the sentry_flutter package into the app.
|
The sentry package makes it easier to send
|
error reports to the Sentry error tracking service.To add the sentry_flutter package as a dependency,
|
run flutter pub add:<topic_end>
|
<topic_start>
|
3. Initialize the Sentry SDK
|
Initialize the SDK to capture different unhandled errors automatically:
|
<code_start>import 'package:flutter/widgets.dart';
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.