此方法将调用无状态小部件内的 init 函数。 但是如何模拟调用 dispose 函数呢?
var widget = StatelessWidgetExample();
await tester.pumpWidget(widget);
我也尝试模拟从树中移除。
await tester.pumpWidget(widget);
await tester.pumpWidget(Container());
但是没有用
答案 0 :(得分:0)
是这样的
var key2 = Key('a');
var testStateful = _TestStateful(
key: key2,
child: TestInitDispose(),
);
await tester.pumpWidget(testStateful);
/// will call init
var state = tester.firstState<__TestStatefulState>(find.byKey(key2));
state.remove();
await tester.pump();
/// will call dispose
});
...
class _TestStateful extends StatefulWidget {
final Widget child;
const _TestStateful({Key? key, required this.child}) : super(key: key);
@override
__TestStatefulState createState() => __TestStatefulState();
}
class __TestStatefulState extends State<_TestStateful> {
bool showChild = true;
void remove() {
setState(() {
showChild = false;
});
}
@override
Widget build(BuildContext context) {
return showChild ? widget.child : Container();
}
}