Empty set state what is the point?

Viewed 6019

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(() {});
  }
3 Answers

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';
   }
  });
}

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

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

  1. Assertions. If any assert fails, throws exception and stops there.
  2. Execute the callback function (final dynamic result = fn() as dynamic;)
  3. Ask framework to rebuild(_element.markNeedsBuild();)

The documentation says [ https://docs.flutter.io/flutter/widgets/State/setState.html ]:

Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.

The empty bracket { } is the empty callback (because you apparently don't need one):

The provided callback is immediately called synchronously. [...]

In short:

setState(() {});

is a way to tell the framework to build the state object anew, without using the possibility to pass a callback which would be called right after the build

Adding to the other answers, there is one difference.

When setState() is called on an unmounted (mounted == false) widget, it will fail. This means that whatever is wrapped inside the setState callback won't be called, whereas if you would run it outside setState it would be executed.

Related