Inherited models – optimizing inherited widgets
If you add print
statements in the build
methods of MyTextWidget
and MyButton
, you will notice both getting printed every time the counter is incremented. However, the counter is updated only in MyTextWidget
and MyButton
has no use of the counter value. Wouldn't it be great if only those widgets which consume the value are rebuilt rather than every widget which is using the .of
method? Inherited models to the rescue!
Inherited models are used in place of the inherited widget extension. These models provide the aspect
value, which allows the provisioning of rebuilding the selected widgets and not the whole widget tree. Let's update our code a little in order to achieve what we expect.
MyInheritedWidget
should now extend InheritedModel<int>
instead of an InheritedWidget
. As soon as you do that, it will ask you to override a compulsory method named updateShouldNotifyDependent
. The overall class should now look...