Flutter setState callBack fn方法区别
区别
setState(() {
_counter++;
});
_counter++;
setState(() {
});
看源码:
void setState(VoidCallback fn) {
assert(fn != null);
assert(() {
if (_debugLifecycleState == _StateLifecycle.defunct) {
//throw
}
if (_debugLifecycleState == _StateLifecycle.created && !mounted) {
//throw
}
return true;
}());
final Object? result = fn() as dynamic;
assert(() {
if (result is Future) {
//throw
}
return true;
}());
_element!.markNeedsBuild();
}
从上可以看出来,两种写法,本质上没有什么区别。
都是执行 _counter++; 语句之后,
调用 _element!.markNeedsBuild(); 刷新界面
网友评论