8

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(() {});
  }
floydheld
  • 314
  • 2
  • 11
Aya Elsisy
  • 2,029
  • 4
  • 13
  • 23

3 Answers3

9

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();)
Dinesh Balasubramanian
  • 20,532
  • 7
  • 64
  • 57
3

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

floydheld
  • 314
  • 2
  • 11
  • 7
    The style guide (somewhere) says not to do this, because it should be clear from looking at the `setState` about *why* you are calling it, and if things get refactored such that the code is no longer needed here, the corresponding `setState` call can also be refactored out. – Randal Schwartz Sep 19 '18 at 19:54
  • 1
    I think "right after the build" part is not correct. It should be "just before rebuilding". – Karol Zlot Mar 15 '21 at 22:46
  • 1
    https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#comment-empty-closures-to-setstate – Monday Fatigue Feb 20 '22 at 13:31
1

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.

Magnus
  • 17,157
  • 19
  • 104
  • 189