skip to Main Content

I want to know setState the difference in order.

I write simple code below.

setState(() {
  _counter++;
});
_counter++;
setState(() { });

I think setState frequency will affect the memory leak.
I want to create a more stable version of the Flutter app.

2

Answers


  1. _counter++ is increasing a counter variable this is supposedly part of the state of a StatefulWidget

    If you do it outside of setState() function, that change will not be taken into account, and will not force a rebuild of the widget.

    I can’t say anything about a possible memory leak, because I know nothing about that, and I doubt this happens, because this the way of doing things. And without any more code, that’s impossible to say.

    Login or Signup to reply.
  2. It’s better to call setState() with the updated value because it ensures that the widget is rebuilt with the correct state. If you call setState() without passing any updated value, it may lead to inconsistencies between the widget’s state and its visual representation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search