空置状态有什么意义?

编程入门 行业动态 更新时间:2024-10-17 08:22:05
本文介绍了空置状态有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道在不为变量设置新值的情况下调用setState的要点.

I want to know the point behind calling setState without setting a new value to the variables.

readLocal() async { prefs = await SharedPreferences.getInstance(); id = prefs.getString('id') ?? ''; if (id.hashCode <= peerId.hashCode) { groupChatId = '$id-$peerId'; } else { groupChatId = '$peerId-$id'; } setState(() {}); }

推荐答案

我会说这只是一个惯例.上面可以改写为

I would say it's just a convention. The above can be re-written as

readLocal() async { prefs = await SharedPreferences.getInstance(); setState(() { id = prefs.getString('id') ?? ''; if (id.hashCode <= peerId.hashCode) { groupChatId = '$id-$peerId'; } else { groupChatId = '$peerId-$id'; } }); }

两者都会做同样的事情.对state variable进行突变后再调用setState(() {})看起来很简洁而且很容易实现.

Both will do the same thing. Calling setState(() {}) after mutating the state variable looks neat and reabable.

根据setState的实现部分,将按顺序排列下面的内容.

As per the implementation section of setState, it will below things in order.

  • 断言.如果任何断言失败,则引发异常并在那里停止.
  • 执行回调函数(final dynamic result = fn() as dynamic;)
  • 要求框架重建(_element.markNeedsBuild();)
  • Assertions. If any assert fails, throws exception and stops there.
  • Execute the callback function (final dynamic result = fn() as dynamic;)
  • Ask framework to rebuild(_element.markNeedsBuild();)
  • 更多推荐

    空置状态有什么意义?

    本文发布于:2023-11-02 16:55:01,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1552919.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:有什么意义   状态

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!