I know that these two are the same.
counter++;
setState((){});
setState((){
counter++;
};
but why does it work?
setState((){});
counter++;
I can see the change of counter even I changed it after calling setState function.
You don't actually need to do count++ inside the setState.
If you look at the documentation/code https://api.flutter.dev/flutter/widgets/State/setState.html, you'll see that the only line that matters is _element!.markNeedsBuild();.
setState() will basically rebuild your widget. And since you modified the instance of your widget with count++, the updated count will appear on the screen.
setState takes a callback and it is recommended to apply your state modifications inside it as it provides some useful error messages that will help you when debugging if you are doing something wrong.
Related
At the beginning of my app I have a global variable gameData which is declared and instantiated as:
GameData gameData = GameData();
Later I want to clear the gameData variable and re-instantiate/reset the variable with a clean instance of GameData. I do this by calling a function:
void ResetGameData() {
gameData = new GameData();
}
But that's not clearing the gameData variable. All the old values are remaining. Is there a better way of doing this?
It seems like your approach should work.
If you're building your UI based off of GameData, you will need to call setState() or notifyListeners() to rebuild everything.
The issue was I was instantiating a class within a class, and that syntax was incorrect so the sub-class was retaining its previous data. The rest of the variable was re-instantiated correctly.
I have a GridView that loads lazily at the end of the scroll controller, however I want to load the data when I reach halfway through the scroll controller. If I try to do an if statement that checks if the scroll has reached 50% of the controller it will never become equal, however, if I do a greater than equal to 50% it calls the function multiple times ending in duplicates of data. How can I avoid this duplication or is there a better way to do this.
A simple way to do it is just use your current code and an additional boolean 'wasCalled' which you set to true in the function. But: I'm confident there's better solutions.
I haven't really worked with GridViews, but as far as I understand it builds its children lazily. Have you tried inserting a custom widget in the middle of the children's list?
E.g. in your build function you could:
#override
Widget build(BuildContext _) {
callCustomFunction();
return Container();
}
You can use the "extentAfter" property to know how much more space is left till the end..
I guess this is something you are trying to do..
https://stackoverflow.com/a/49509349/13460232
A const variable will only be created once. When I try to create the same variable, the old variable will be returned. Thus, no new memory allocation occurs.
For example, if I create const Icon(Icons.add) and later try to use const Icon(Icons.add) in another class, the old one will be returned.
But my question is: if I use const Icon(Icons.add) in a stateful widget, will this memory allocation be present forever even after the stateful widget is disposed?
Yes, an instance created using const will never be garbage collected.
The instance is in fact created at the compilation and present in the binary.
This question already has answers here:
Why does setState take a closure?
(2 answers)
Closed 4 years ago.
Why Flutter defines that we should call:
setState(() { _counter++});
instead of:
_counter++;
setState(() {});
As far as I can see in setState() code, it doesn't use anything that's passed as a parameter anyway.
The end result in release mode is the same.
But in debug you get an assert for free that checks that the callback inside setState() does not return a Future and it returns immediately.
But if you are sure the callback is synchronous, the result in debug is the same.
I discovered that when setting a parent true, that parent's children are all called 'OnEnable' function. Even though children are all set true already.
I don't want to call 'OnEnable' function even though it's already set true.
How can I prevent it?
This documentation on execution order may be useful to you.
If you have logic in the child objects that you do not want to call from the parent being set as active, than remove it from OnEnable and hook it up to your own delegate somewhere else.
You are going to have the same thing happen with the Start and Awake functions as well. Personally I would try and find a way to abstract your code a bit more to make sure the child object is not dependent on the parent.
SetActive() behavior is inherited by all of the children of that particular gameObject. In order to solve your problem you most likely have to restructure your GameObject hierarchy.