0

Could you please explain to me at what point during the build the property of dirty changes its value?

Now I will explain what I mean:

When we call setState() during the build, we will not see any effect - setState() will not work. If we fall into the implementation of the setState(), we will see that we call

markNeedsBuild() in which we will just return method if dirty is true.

if (dirty) {
      return;
    }
    _dirty = true;
    owner!.scheduleBuildFor(this);

But during the build it will be true, and we can see this from the documentation:

Since it is inefficient to build an element twice in one frame, applications and widgets should be structured so as to only mark widgets dirty during event handlers before the frame begins, not during the build itself.

So my question is, at what specific point will dirty property become false? (if we didn't any changes to state)

2 Answers2

1

Based on My knowledge when you do setState build method call again rebuild widgets based on the dirty variable it means when we do setState state is dirty means bool variable isDirty which is maintain by framework. So, whenever framork start to rebuild widget tree it will see that whose isDirty variable is change and it will be re-render again. This process you can see during debug so put debug point and see all the properties.

AJJ
  • 46
  • 3
1

The dirty property is used to track if a widget needs to be rebuilt during the current frame.

When calling setState(), the dirty property is set to true for the widget. The framework checks the dirty property during the layout and painting phases to determine if a widget should be rebuilt.

The dirty property becomes false for a widget after it has been rebuilt during the layout and paint phases of the frame.

Hamed
  • 5,867
  • 4
  • 32
  • 56