text stringlengths 1 372 |
|---|
this only prints errors and does nothing else. |
you can customize these behaviors, |
typically by setting them to values in |
your void main() function. |
below each error type handling is explained. at the bottom |
there’s a code snippet which handles all types of errors. even |
though you can just copy-paste the snippet, we recommend you |
to first get acquainted with each of the error types. |
<topic_end> |
<topic_start> |
errors caught by flutter |
for example, to make your application quit immediately any time an |
error is caught by flutter in release mode, you could use the |
following handler: |
<code_start> |
import 'dart:io'; |
import 'package:flutter/foundation.dart'; |
import 'package:flutter/material.dart'; |
void main() { |
FlutterError.onError = (details) { |
FlutterError.presentError(details); |
if (kreleasemode) exit(1); |
}; |
runApp(const MyApp()); |
} |
// rest of `flutter create` code... |
<code_end> |
info note |
the top-level kReleaseMode constant indicates |
whether the app was compiled in release mode. |
this handler can also be used to report errors to a logging service. |
for more details, see our cookbook chapter for |
reporting errors to a service. |
<topic_end> |
<topic_start> |
define a custom error widget for build phase errors |
to define a customized error widget that displays whenever |
the builder fails to build a widget, use MaterialApp.builder. |
<code_start> |
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> |
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); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.