Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - change variables inside or outside setState? [duplicate]

Tags:

flutter

When changing the state in a Flutter Widget, are there any differences between doing this

_variable1 = true;
variable2 = 'abc';
setState(() => {});

or this

setState(() => {
    _variable1 = true;
    variable2 = 'abc';
});

Almost all the examples in the docs use the last one, but I didn't notice any pratical differences. The variables are set and the state is updated in both cases, but I am wondering if there is a scenario that something doesn't work as expected if use one or another.

like image 387
Ricardo BRGWeb Avatar asked Feb 11 '19 19:02

Ricardo BRGWeb


People also ask

What is the use of setState in flutter?

setState is a way to dynamically change the UI. We call it inside the State Object class of the StatefulWidget. Calling setState marks the corresponding Widget dirty. When flutter builds the next frame (approx. every 16ms), it renders the Widget according to the latest values of the State Object. Where we call setState matters a lot.

What is a state object in flutter?

What is a State Object in flutter? setState is called inside a State class. Let's understand this in detail. State is simply the information of a StatefulWidget. Every StatefulWidget has a State Object. This State Object keeps a track of the variables and functions that we define inside a StatefulWidget.

What is a stateful widget in flutter?

Widget: Any UI component on the screen is called a Widget in flutter. A Widget can have its own Widgets, like a tree structure. StatefulWidget: A Widget that can change dynamically. Generally used when we want to modify something on the screen's UI. What is a State Object in flutter? setState is called inside a State class.

How to change the state of a variable in a widget?

If your widget changes the variable or is changed by it, it needs to be in the State. If it is passed to the widget it needs to be stored in the Widget and cannot change. If both (passed to the widget then changeable), put it in both and initialise the State variable from the Widget variable.


3 Answers

Already been posted here this question.

It's a convetion, it doesn't matter, but, it's good pratica to wrap all the changes inside the setState function. But the results will be the same in both cases.

like image 81
Fellipe Malta Avatar answered Oct 06 '22 13:10

Fellipe Malta


According to the flutter docs, it is stated that:

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.

Mainly I think that it is for readability purposes, i.e, you need to put in the body of setState what is changing in the new build of the widget, without mixing that with your computations as the first method in your question does.

like image 43
Sami Kanafani Avatar answered Oct 06 '22 11:10

Sami Kanafani


I've read somewhere that an empty setState is a "code smell", because the body of the callback should indicate the reason for the setState.

like image 2
Randal Schwartz Avatar answered Oct 06 '22 13:10

Randal Schwartz