我正在将Flutter Provider与DiagnosticableTreeMixin配合使用,以便跟踪调试器中的状态。
但是我需要设置DiagnosticableTreeMixin才能使用列表。
例如
1)模型设置
class MyClass with DiagnosticableTreeMixin {
MyClass({this.value1, this.value2});
final String value1;
final String value2;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('value1', value1));
properties.add(StringProperty('value2', value2));
}
}
2)提供商设置
class MyClassProvider with ChangeNotifier, DiagnosticableTreeMixin {
List<MyClass> _listOfMyClass = [];
List<MyClass> get listOfClass => _listOfMyClass;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<List<MyClass>>(
'ListOfMyClass', _listOfMyClass));
}
}
在调试器中,当我打开MyClassProvider并查看该值时,会看到类似以下内容的
:ListOfMyClass:[MyClass#0bb46(value1:"123",value2:"345),MyClass#0b546(value1:"123",value2:"456"),MyClass#01b46(value1:"1
这是一个具有3个实例的示例,它总是被切成一行。尝试跟踪多个值时,这是有问题的,因为它们根本不会显示,并且它不是可扩展的结构,因此以这种方式跟踪任何内容都是很麻烦的。
如何在调试器中将它们转换为可读的值?
类似的东西
ListOfMyClass:
[
MyClass#0bb46(value1:"123",value2:"345),
MyClass#0bb46(value1:"123",value2:"345),
MyClass#0bb46(value1:"123",value2:"345),
MyClass#0bb46(value1:"123",value2:"345),
MyClass#0bb46(value1:"123",value2:"345),
MyClass#0bb46(value1:"123",value2:"345)
]
答案 0 :(得分:0)
使用简单
IterableProperty
或自己创建课程 (有关更多信息,请参见flutter / lib / src / foundation / diagnostics.dart:2577)
一些例子
class _SendPostPageState extends State<SendPostPage>
with DiagnosticableTreeMixin {
bool isLoadig = true;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
// properties.add(isLoadig);
properties.add(IterableProperty('Array', [isLoadig]));
}
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Color(0xff262626),
borderRadius: BorderRadius.vertical(
top: Radius.circular(32),
),
),
child: ListView(
controller: widget.scrollController,
children: [
Center(
child: SizedBox(
height: 10,
width: 10,
child: Material(
color: Colors.amber,
),
),
)
],
),
);
}
}