Flutter setState rebuilding all children - flutter

I've got a parent widget that has a Stack with two children.
When using setState in the parent, without actually changing something (setState with empty body) why are the build methods of the children being called?
Is it not the case that flutter rebuilds only the widgets with dirty params?

Flutter will run the build function for all children (exception being const Widgets). It will not rerender them though. If Flutter finds that nothing changed in the widget, it will just reuse the previous render.
The program has no way of knowing if state has changed, unless it checks by running build.

Related

HookConsumerWidget in flutter causing dispose errors

In my flutter app, i use a list view inside a HookConsumerWidget, this list has a filter and once it filters the list tiles have big gaps, and i see this error:
This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
i couldn't solve it because its a HookConsumerWidget and its not flexible
try to show some code snippets of the problem or the log messages,
my concern is when you're using HookConsumerWidget you don't need to use setState as you could manage the UI changes directly with listeners or watcher with 'StateProvider' for example.

Do adding, removing a child to stack cause all stack children to rebuild?

Let's say I have a stack of 100 positioned widget children, I add one new positioned widget to that stack but the rest don't change, then I setState to rebuild that stack, should all 100 children run their rebuild function? I ask because this is happening in my app but was hoping I could prevent the rebuild of the entire stack, since flutter should see most of the stack as unchanged.
edit: [more accurately, how do i make sure i'm not re-rendering all 100 widgets in the stack when i add one child or remove one child, is there a way to verify, inspect the render tree]
All the widgets inside a stateful widget should be rebuild in every setState,
To prevent of setState some widgets you should have a separate stateful widget for the stack.
this may useful

How to rebuild parent widget on dispose of child widget?

I upgraded the version of Flutter in my desktop.
At the same time, there was a problem that was not before.
Before, The parent statefulwidget(scaffold) was re-built when the user popped the child statefulwidget(scaffold in pushed route).
However, now(1.20.3) It doesn't.
So, is there any update to call build method of the parent widget on dispose of child widget?
I think that changing the version of Flutter to the old one is not a progressive approach.
(And I didn't check the version in fact..)

How can I see when a Widget rebuilds?

Code source
Is there any trick to know that a widget has been rebuilt?
As a demonstration, i.e. if we randomly colored the widgets on every rebuild, it would look like this:
Flutter actually has built-in functionality for exactly what you are trying to achieve in the DevTools inspector:
This is called the Repaint Rainbow and it can be enabled in Android Studio, i.e. IntelliJ, as demonstrated above or directly in Dart DevTools:
Repaint Rainbow
Shows rotating colors on layers when repainting.
From the linked article
Notes
There can be many reasons for repaints and seeing a widget rebuild does not inherently mean that you triggered the rebuild as it can also come from somewhere else in the tree.
You cannot know if a widget has been rebuilt in code because that is against how the framework works - you can obviously catch any build or paint calls by integrating that into your build or paint function, but you should really not do that because builds and paints should be idempotent.
If you use android studio you can open Flutter Performance and checked Track widgets rebuild in Widget rebuild stats
Every time the widgets are rebuild ,the build() is called So You can write a print() in your build() and track when the widgets are getting rebuilt
You can't know it, and should not build a word around to obtain that value yourself either.
This is anti pattern, as the number of times a widget rebuilt should never have an impact on the output.

How often is the build method called?

I noticed that the build method gets called often in a flutter app.
I know that if the states of the page change in a statefulWidget, the build method gets triggered. But I also noticed that the build method is called even if nothing is changed in the app.
Considering the case where you leave the app to itself, is it normal for the build method to get called frequently? If so, why and how often?
Why
The build method is called any time you call setState, your widget's dependencies update, or any of the parent widgets are rebuilt (when setState is called inside of those).
Your widget will depend on any InheritedWidget you use, e.g. Theme.of(context), MediaQuery.of(context) etc.
This means that if the theme changes for example or the screen orientation swaps, your widget will also be rebuilt.
When you use widgets like MaterialApp, Scaffold etc. that are provided by the framework, your widget will be rebuilt a lot because these parent widgets depend on many InheritedWidget's and then are rebuilt, which causes your widget to be rebuilt as well.
How often
There is no number for how many rebuilds are "normal" as this completely depends on your tree size and most importantly widgets are in that tree. If you were to run runApp(Container()), there would be no rebuilds.
Just keep in mind that all of these rebuilds probably have a good reason to occur and Flutter is built for this, so you do not need to worry about this.
The only point you should start worrying is when you have constant rebuilds that are probably caused by some builder (which calls setState internally) you are using incorrectly.
Exactly when
The documentation lists all specific cases when rebuilds can occur:
After calling initState.
After calling didUpdateWidget.
After receiving a call to setState.
After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
After calling deactivate and then reinserting the State object into the tree at another location.
Rebuilds from parent widgets
If you want to understand how InheritedWidget works, see this answer. It also touches when a rebuild in a parent widget causes the subtree to rebuild.
After calling initState.
After calling didUpdateWidget.
After receiving a call to setState.
After a dependency of this State object changes (e.g., an InheritedWidget referenced by the previous build changes).
After calling deactivate and then reinserting the State object into the tree at another location.
Read this for more info https://api.flutter.dev/flutter/widgets/State/build.html