栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Flutter中使用setState时?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Flutter中使用setState时?

根据文档:

调用setState会通知框架此对象的内部状态已更改,该方式可能会影响此子树中的用户界面,这将导致框架为该State对象安排构建。

因此,如果小部件的状态更改 ,则 必须调用

setState
以触​​发视图重建,并立即查看新状态所隐含的更改。

无论如何,以下片段都是等效的。

第一种情况(直接形式

flutter create <myproject>
):

class _MyHomePageState extends State<MyHomePage> {  int _counter = 0;  void _incrementCounter() {    setState(() {      // This call to setState tells the Flutter framework that something has      // changed in this State, which causes it to rerun the build method below      // so that the display can reflect the updated values. If we changed      // _counter without calling setState(), then the build method would not be      // called again, and so nothing would appear to happen.      _counter++;    });  }

第二种情况:

class _MyHomePageState extends State<MyHomePage> {  int _counter = 0;  void _incrementCounter() {    _counter++;    setState(() {});  }

我不知道的原因是,如果第一种情况是常规的使用方式

setState
,我会说是因为代码的可读性。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号