How can I see when a Widget rebuilds? - flutter

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.

Related

Flutter Multiple Widget Remove

How to remove multiple widgets at once in flutter while working on vs code?
I know what you're talking about, but in VS Code you can only delete one widget at a time.

How Flutter framework optimizes the widget rendering if build method can be called many times?

I have a simple question. In Flutter docs it is explained build method can be called many times by framework and it is recomended that we should only return widgets through the build method. But even we return only widgets, isn't it causing some performance issues? If build method is called 20 times in a second, does not it mean widgets will be rendered 20 times and should not it be slow? If Flutter framework optimizes rendering widgets, how?
Can you please reference some detailed docs or answers for that?
This is a really good question. The Flutter team has put together this really useful video on YouTube that explains this perfectly and in details. Essentially you need to understand the difference between the Widget tree and the element tree. Every widget inside your widget tree is backed by something called an element, and this element is what Flutter actually uses to draw "things" on the screen. The widgets are the visual representation of the elements from the programmer's perspective while the elements are the actual visual representation of the widgets on the screen. The optimizations are done at the element tree level which you usually don't get to work with directly.
Check out that video, there is a lot of useful information here
Here is the explanation about how Flutter handles render optimization thanks to the suggested videos and Flutter docs.
Flutter does not only have widget trees. For each visual representation on UI there is actually 3 main trees which are Widget Tree, Element Tree and Render Objects.
Widget Tree is hierarchy of widgets and includes all the widgets created. But since widgets are immutable and changed in the tree over time, there should be another tree called Element Tree which keeps the states and optimizes widget rendering.
Element Tree is the tree which Flutter uses to render widgets on the screen and is not changed a lot over time same as Widget Tree because whenever a widget is changed in the Widget Tree, if the new widget has the same type as before, element tree keeps the element for the corresponding widget. For example if you only change a Text widget's text in the widget tree, Widget Tree is going to be changed but Element Tree is going to stay same because Text widget still has the same runtimeType as before.
So that render optimization is mostly about reusability. Flutter framework is trying to reuse the objects as much as it can. Even the Widget Tree is changed a lot over time, Element Tree is not changed a lot same as the Widget Tree and use the same element object if the widget type is not changed.

Why do we need stateless widget in flutter?

As the below diagram shows Stateful widget covers what Stateless widget do, When I can do the same thing with Stateful widget, why Flutter designers added another widget? For increasing performance?
Please do not post the difference just answer the Why?
Would you always use a sword to do a knife's job? Given that you always have both of them available with easy access. The knife can't do everything a sword can, but would that mean that a sword should be used by default for everything, since it's more robust?
You'll have many widgets where the state of the elements will not change, and others where you will not have to use setState to update your UI either(given that you would rather not use higher level state management solutions).
Using stateless widgets as long as they can do the job, means more performent widget trees.
Please do not post the difference just answer the Why?
Knowing the difference between them is key to understanding 'why', and the differences are not just in the name less vs full. But sensing that you aren't interested, that's why.
Good Flutter code seperates the code into many small Widgets. In addition to better performance creating a StatefulWidget means that you have to write more code and add unnecessary complexity to your code.
For better performance. Stateless widgets are usually only called in one of these three situations:
1- widget is inserted in the tree.
2- Widget's parent changes its configuration
3- Inherited Widget it depends on changes.

In Flutter does avoiding setState() achieve anything?

In Flutter does avoiding setState() achieve anything?
I have a StatefulWidget (screen) where I use Widgets that handle their own state completely with the exception of the FAB button. The Fab button is disabled or enabled when data has changed in order to allow the update of the DB when data has changed. I posted a question on SO as to how to use Provider to handle that and thus prevent the need to setState(), and implemented that with Provider.
When looking at another screen to implement something similar, I wondered if another solution may be better. This screen also contains Widgets that all handle their own state - TextFields with TextEditControllers, so the only place I was calling setState() was to enable or disable the FAB. I had previously created a Stateful Widget to create a CheckBox with its own state because the standard CheckBox doesn't have state. It's fairly simple and only 50 lines of code. So, I did the same with the FAB, I created a Stateful Widget (CustomFab) that encapsulates a FAB. When it needs to be enabled or disabled, it is called to set its own state, and returns a FAB that is either enabled or disabled with the appropriate color. Because I have other similar screens, I can use that same component with them.
Does it achieve anything by avoiding setState() in this situation (for mobile and Web), and if so, which is the best way to handle that (Custom-Widget or Provider or another)?
setState is merely avoided in Flutter development for two main reasons:
It rebuilds the whole widget, which may hinder performance on consecutive calls if not used correctly.
using setState only as a state management couples the business logic to the UI, which may reduce code re-usability and maintainability.
In simple apps, there are no significant issues in using setState. However, if you need more control over your code, I suggest going for more complex state management patterns.

Should screens be Stateless or Stateful?

I'm developing an app with lots of screens and pages. I've read somewhere that you should use Stateless Widgets whenever you can.
Why is that?
If I have a lot of screens, should those be stateless? And then the content inside be Stateful? Is it better to have both the screen and widgets inside being Stateful?
You should ask yourself some questions about the screen/page to decide if it will be Stateless or Stateful.
The most obvious, does it need to change state?
Do you need to call initState, didChangeDependencies or another lifecycle method?
Is a bad practice to make Stateful when not needed. A good idea might be to start always as Stateless widget and if needed you can change it to Stateful easily with Alt + Enter shortcut (Android Studio).
I always start by creating a Stateless Widget and work with it until I have to change something state. So I could quickly use `Alt-Enter/ Convert to a Statefull from Intellij/AS to change it to stateful. (doing the inverse is not so easy, so...).
Moreover, if you use Stateful widget with some async mechanism, like streams, you could build the widget once and use the streams to update the information you need, and it will not impact the performance of your app so much. But if you call setState many times, this can degrade your App, since for every setState the Widget tree will be rebuilt.
This article from the flutter docs shows interesting tips about handling state change in flutter apps: