TOP(About this memo)) > 一覧(Flutter) > エラーハンドリング
No | ハンドリングするエラーの種類 | ハンドリングするためのAPI |
---|---|---|
1 | フレームワークの処理によるエラー | FlutterError.onError |
2 | フレームワーク外のエラー | PlatformDispatcher.instance.onError |
3 | レンダリング失敗時 ※デフォルトの挙動をカスタマイズ |
ErrorWidget.builderで 挙動をカスタマイズ |
// flutter/lib/src/widgets/framework.dart
static ErrorWidgetBuilder builder = _defaultErrorWidgetBuilder;
//...
static Widget _defaultErrorWidgetBuilder(FlutterErrorDetails details) {
String message = '';
assert(() {
message = '${_stringify(details.exception)}\nSee also: https://flutter.dev/docs/testing/errors';
return true;
}());
final Object exception = details.exception;
return ErrorWidget.withDetails(message: message, error: exception is FlutterError ? exception : null);// 渡しているexceptionは インスペクタ表示用で画面に表示はされない。
}
void main() {
// ErrorWidget.builderの実行時にはFlutterError.onErrorも呼ばれるため、この中でエラーレポートは不要。
ErrorWidget.builder = (FlutterErrorDetails details) {
if (kDebugMode) {
return ErrorWidget(details.exception);
}
return Container(
alignment: Alignment.center,
child: Text(
'Something error happened!',
style: const TextStyle(color: Colors.yellow),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
),
);
};
FlutterError.onError = (errorDetails) {
debugPrint("Flutter framework error occured: $errorDetails");
// エラーレポート送信等の処理
// ...
};
PlatformDispatcher.instance.onError = (error, stack) {
debugPrint("Error occured: $error");
// エラーレポート送信等の処理
// ...
return true;
};
// ...
}
class ErrorWidget extends LeafRenderObjectWidget {
// ...
RenderBox createRenderObject(BuildContext context) => RenderErrorBox(message);
static ErrorWidgetBuilder builder = _defaultErrorWidgetBuilder;
static Widget _defaultErrorWidgetBuilder(FlutterErrorDetails details) {
// ...
final Object exception = details.exception;
return ErrorWidget.withDetails(message: message, error: exception is FlutterError ? exception : null);
// ...
}
// ...
}
class RenderErrorBox extends RenderBox {
// ...
late final ui.Paragraph? _paragraph;
void paint(PaintingContext context, Offset offset) {
// ...
context.canvas.drawParagraph(_paragraph, offset + Offset(left, top));
}
// ...
}