With Flutter 3.10, which is the better way to get the `Size` with `MediaQuery`

1.6k views Asked by At

Flutter 3.10 has come with several improvements for MediaQuery use. And I see that we can for example get the Size using MediaQuery.of(context).size as usual, but now we can use MediaQuery.sizeOf(context).

Which is more performant? The latter? Which is the recommendation?

EDIT (2023-06-15): I'm refining the question. The core of the question is not about getting a Size, this is just a simple example of the new ways of using MediaQuery new ways of getting the same data using new available methods. That could be with MediaQuery.of(context).platformBrightness- MediaQuery.platformBrightnessOf(context) or another one.

1

There are 1 answers

0
Carlos Daniel On

After reading some resources, what @peter-koltai shared as a comment is pretty accurate.

Then the answer could be summarized as follow (Summary taken from Andrea Bizzoto):

By calling MediaQuery.of(context).size/padding/orientation, the widget will rebuild when any of the MediaQuery properties change (less performant). By calling MediaQuery.sizeOf/paddingOf/orientationOf(context) the widget will rebuild only when the property changes, avoiding unnecessary rebuilds.

Take a look of this issue where the concept is well explained.