Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Does it matter what code is in setState()?

Tags:

When we want a StatefulWidget to rebuild we call setState() but does it really matter if the code we type is inside that function or outside of it?

Is this:

class _ShoppingListState extends State<ShoppingList> {
  Set<Product> _shoppingCart = new Set<Product>();
  void _handleCartChanged(Product product, bool inCart) {
    setState(() {
      if (inCart)
        _shoppingCart.add(product);
      else
        _shoppingCart.remove(product);
    });
  }
}

the same as this:

class _ShoppingListState extends State<ShoppingList> {
  Set<Product> _shoppingCart = new Set<Product>();
  void _handleCartChanged(Product product, bool inCart) {
    if (inCart)
      _shoppingCart.add(product);
    else
      _shoppingCart.remove(product);
    });
    setState((){});
  }
}
like image 826
Jus10 Avatar asked Apr 23 '18 11:04

Jus10


People also ask

How does setState work in Flutter?

setState method Null safety Notify the framework that the internal state of this object has changed. Whenever you change the internal state of a State object, make the change in a function that you pass to setState: setState(() { _myState = newValue; }); The provided callback is immediately called synchronously.

What can I use instead of setState in Flutter?

The short answer is that just add currentIndex as one of the parameter inside your navBarSection .

How do I use setState in stateful widget?

When you change the state of a Stateful Widget, use setState() to cause a rebuild of the widget and its descendants. You don't need to call setState() in the constructor or initState() of the widget, because build() will be run afterward anyway. Also don't call setState() in synchronous code inside build().

Does setState rebuild entire page?

If you call setState() on WidgetB it'll rebuild itself and it's descendants, no matter if they are Stateless or Stateful Widgets.


2 Answers

According to the docs:

@protected

void setState (
    VoidCallback fn
)

Notify the framework that the internal state of this object has changed.

Whenever you change the internal state of a State object, make the change in a function that you pass to setState:

setState(() { _myState = newValue });

The provided callback is immediately called synchronously. It must not return a future (the callback cannot be async), since then it would be unclear when the state was actually being set.

And also

Generally it is recommended that the setState method only be used to wrap the actual changes to the state, not any computation that might be associated with the change.

It's doesn't explicitly say any thing about if it's different to call is in or outside the function. But it does recommend to put it inside. And in my opinion it's also more readable and logical if you put the changes in side the setState function

like image 156
Ron Nabuurs Avatar answered Sep 25 '22 00:09

Ron Nabuurs


You should do all your mutations inside the closure, and the computing outside of the closure.

like image 29
Rémi Rousselet Avatar answered Sep 26 '22 00:09

Rémi Rousselet