Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter 2: Best practice using/store the size of the phone

I am trying to find the best practice for using some casual data.

For example, I have seen in some videos (for example this one), people storing their size at the beginning of their page and sending it as a parameter for every stateless widget they have.

Size size = MediaQuery.of(context).size;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Is it better for the performances (even if it’s a really little) to do like this or is it better to access it every time via MediaQuery.of(context).size ?

>Solution :

calling MediaQuery.of(context) is in order of n where n is the length of your widget tree. so it’s not that bad for most cases. also using MediaQuery.of(context) and not passing along constructors makes your code cleaner. but your widgets should not depend on the size of the screen. in most cases widgets like Flex, Expanded, FitedBox and .. can help you achieve this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading