flutter - Flutter 中的 OnStateChanged 方法
问题描述
在 Flutter 中,是否有类似onStateChanged的方法在页面状态发生变化时被调用?
setState(() {
widget._loadCompleted = true;
widget._loading = false;
});
我正在尝试在setState()
方法中设置两个布尔值。我出于其他几个原因设置状态。所以我想知道最后一次状态变化是否是出于这个特殊原因。
解决方案
正如 Günter 所说,没有像onStateChanged()
. 你必须用build()
方法处理它。
如果我说得对,你可以这样使用:
class _MyAppState extends State<MyApp> {
bool myFlag = false; // initially set to false
void _doYourWork() {
setState(() => myFlag = true); // set to true here
}
@override
Widget build(BuildContext context) {
if (myFlag) {
// setState() just got called
} else {
// we are building fresh for the first time.
}
myFlag = false;
return yourWidget();
}
}
在此之后build()
将收到myFlag
值,true
然后可以false
再次设置为。所以,你可以做到这一点。
推荐阅读
- android - FusedLocationProviderClient - 应用停止在前台服务上接收位置更新
- python - 如何在 Pandas 中将数据框行拆分为两行?
- reactjs - mui 的按钮组件中显示的将材质 ui 迁移到 mui 错误。在遵循有关迁移的文档之后
- tensorflow - 从 C++ 加载 tensorflow 自定义操作
- javascript - RxJS - 按顺序处理 HTTP 请求
- python - 使用烧瓶在 html 页面上未播放音频
- python - Python:为什么我无法使用 cv2 的功能?
- c# - 使用 C# 如何从 xaml 返回切换开关的值
- hibernate - Hibernate OneToMany - 尽管正确指定了关系,但子实体没有得到持久化
- amazon-web-services - 詹金斯管道中的这个模块需要 boto3 和 botocore