When updating the value of a variable, it must be done inside setState ??

3,684 views
Skip to first unread message

Leonardo Mezavilla

unread,
Jun 11, 2019, 12:55:35 AM6/11/19
to Flutter Development (flutter-dev)
Hi folks!

I have a question about setState:

When updating the value of a variable, it must be done inside setState ??

I did 2 examples updating outside of setState and then called it just to render the screen, and it ran perfectly.  

What do you think about that?  Is it possible that in a moment of greater data flow or in an asynchronous function I can have problems doing that way, or is it all good?


Capture.PNG






import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Petri Dish App'),
),
body: ClassWithStateFull(),
),
);
}
}

class ClassWithStateFull extends StatefulWidget {
@override
_ClassWithStateFullState createState() => _ClassWithStateFullState();
}


class _ClassWithStateFullState extends State<ClassWithStateFull> {
int _cont = 0;

//function with documentation recomendated way
addCountInsideState() {
setState(() {
_cont++;
});
print("I add 1 on count, now it is $_cont");
}

//function add count and render
addCountAndThenCallState() {
_cont++;
setState(() {});
print("I add 1 on count, now it is $_cont");
}

//function add count and render in another function
addCountAndCallStateJustToRender() {
_cont++;
reRenderThePage();
print("I add 1 on count, now it is $_cont");
}

//function to render the page
reRenderThePage() {
setState(() {});
print("runing setState to rerender page");
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Text('new count value is: $_cont'),
RaisedButton(
onPressed: addCountInsideState,
child: Text("addCountInsideState"),
),
RaisedButton(
onPressed: addCountAndThenCallState,
child: Text("addCountAndThenCallState"),
),
RaisedButton(
onPressed: addCountAndCallStateJustToRender,
child: Text("addCountAndCallStateJustToRender"),
)
],
);
}
}

Larpoux

unread,
Jun 11, 2019, 4:45:36 AM6/11/19
to Flutter Development (flutter-dev)


Le mardi 11 juin 2019 06:55:35 UTC+2, Leonardo Mezavilla a écrit :
Hi folks!

I have a question about setState:

When updating the value of a variable, it must be done inside setState ??

I did 2 examples updating outside of setState and then called it just to render the screen, and it ran perfectly.  

What do you think about that?  Is it possible that in a moment of greater data flow or in an asynchronous function I can have problems doing that way, or is it all good?




I am also asking this question. Since I began to work on Flutter I am wondering why this setState. I Confirm that my tries to move data outside setState works perfectly ... ( until now ;-) )
I would be glad if someone can answer this very basic question

Andy Greenshaw

unread,
Jun 11, 2019, 4:55:09 AM6/11/19
to Flutter Development (flutter-dev), Larpoux
You don’t have to use setState for all variables - just those that change the state of the object/widget (eg the change of the variables value will cause something different to be shown on screen, so the screen needs to be rebuilt by the underlying framework). 

Also, you can update many variables inside one setState if that is what is required, or you can update the variables outside setState, and then call setState() {} (an empty call), which will have the same effect - the setState effectively means “rebuild me".

So use setState to notify the framework that the internal state of this object (widget) has changed.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/7737465b-b1e5-4840-93bb-b79ab5051914%40googlegroups.com.

Larpoux

unread,
Jun 11, 2019, 6:11:06 AM6/11/19
to Flutter Development (flutter-dev)
Our question is not that you have to call 'setState' when some data is changed. This is completly understable that you have to redraw the screens when some data is changed.
The question is why this dummy parameter to 'setState' ?  Even the Flutter basic example ( incrementCounter() ) works perfectly without it.

Ian B

unread,
Jun 11, 2019, 6:24:41 AM6/11/19
to Larpoux, Flutter Development (flutter-dev)
My understanding is that it doesn't matter in terms of computation & end result, but it does make the intention clearer for readability, and specifically why setState is being called.

On Tue, Jun 11, 2019 at 11:11 AM Larpoux <lar...@gmail.com> wrote:
Our question is not that you have to call 'setState' when some data is changed. This is completly understable that you have to redraw the screens when some data is changed.
The question is why this dummy parameter to 'setState' ?  Even the Flutter basic example ( incrementCounter() ) works perfectly without it.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Steven McDowall

unread,
Jun 11, 2019, 7:04:57 AM6/11/19
to Ian B, Larpoux, Flutter Development (flutter-dev)

That is correct -- I, in many instances, compute the variables (one or more) outside the setState() .. especially longer running ones w/ asyncs etc. Then just call the setState() to force the build() to re-fire. 


Larpoux

unread,
Jun 11, 2019, 9:14:54 AM6/11/19
to Flutter Development (flutter-dev)
On Jun 11, 2019, at 6:24 AM, Ian B <ibri...@gmail.com> wrote:

My understanding is that it doesn't matter in terms of computation & end result, but it does make the intention clearer for readability, and specifically why setState is being called.

I really do not agree with the  readability. Lambda functions are very powerful but hard to read and understand, and must be avoid when not necessary.
It seems that Dart and Flutter are so proud of their Lambda functions that they put them everywhere.

But maybe this setState parameter can be useful for the Hot Reload feature of Flutter, to know when it has to redraw the screen. This is just a supposition.
The Hot Reload feature is so great that it is ok to add some complexity in the code. But this reason is just something of my imagination.

Remi Rousselet

unread,
Jun 11, 2019, 9:23:06 AM6/11/19
to Flutter Development (flutter-dev)

Larpoux

unread,
Jun 12, 2019, 7:01:16 AM6/12/19
to Flutter Development (flutter-dev)
Thank you Remi for your link.
I am probably now less stupid about this dummy parameter to _setSstate.
This post confirm what I said :
Flutter and Dart are perhaps too complicate.

I read your link one time very fast and I dit not understand anything.
I read your link another timer and it was not really better.
Probably I will understand if I read this link another third time, but I do not have time to loose with stupidities.

Lambda functions (their new name are 'closures') are difficult to understand.
Asynchronous computing is also something difficult.
But when you mix those two concepts you are getting something un-understandable.

A good language must be master by a Pascal programmer.
A good program must be able to produce by a Pascal programmer.

Neither Flutter and Dart meet those criteria.
I will continue to work with Flutter, but I really think that everything could have been much simpler.
Reply all
Reply to author
Forward
0 new messages