Flutter: debugFillProperties() でデバッグを楽にする
June 10, 2024
StatefulWidget(の State)は debugFillProperties()
メソッドを持っています。これをオーバーライドするとデバッグが少しだけ楽になります。
- debugFillProperties method - State class - widgets library - Dart API
- https://api.flutter.dev/flutter/widgets/State/debugFillProperties.html
これを使うことで何がどう変わるのかは以下の Stack Overflow が分かりやすいです。
- dart - When should I override debugFillProperties in Flutter? - Stack Overflow
- https://stackoverflow.com/questions/69187204/when-should-i-override-debugfillproperties-in-flutter
なお、diagnostic_describe_all_properties
のリントを有効にするとこのメソッドをオーバーライドする記述を自動で追加してくれます。
コード例: diagnostic_describe_all_properties
を有効にする前
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
super.key,
});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(title: 'My App'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
required this.title,
});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: MyTextWidget(
_counter.toString(),
maxLines: 1,
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
setState(() {
_counter++;
});
},
),
);
}
}
class MyTextWidget extends StatelessWidget {
const MyTextWidget(
this.text, {
super.key,
required this.maxLines,
this.style,
});
final String text;
final int maxLines;
final TextStyle? style;
@override
Widget build(BuildContext context) {
return Text(
text,
maxLines: maxLines,
style: style,
);
}
}
コード例: diagnostic_describe_all_properties
を有効にする
analysis_options.yaml
include: package:flutter_lints/flutter.yaml
linter:
rules:
- diagnostic_describe_all_properties
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
super.key,
});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(title: 'My App'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
required this.title,
});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('title', title));
}
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: MyTextWidget(
_counter.toString(),
maxLines: 1,
),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
setState(() {
_counter++;
});
},
),
);
}
}
class MyTextWidget extends StatelessWidget {
const MyTextWidget(
this.text, {
super.key,
required this.maxLines,
this.style,
});
final String text;
final int maxLines;
final TextStyle? style;
@override
Widget build(BuildContext context) {
return Text(
text,
maxLines: maxLines,
style: style,
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(StringProperty('text', text))
..add(IntProperty('maxLines', maxLines))
..add(DiagnosticsProperty<TextStyle?>('style', style));
}
}