Fix 'MediaQueryData.fromWindow' is deprecated and shouldn't be used in Flutter

- Andrés Cruz

En español
Fix 'MediaQueryData.fromWindow' is deprecated and shouldn't be used in Flutter

In this post we will see how to correct the error of:

'MediaQueryData.fromWindow' is deprecated and shouldn't be used

MediaQueryData.fromWindow is a Flutter function that is used to get information about the window in which the application is running, this window can be a mobile device, a browser or a desktop application, really any device supported by Flutter. It is usually used to get the size of the window by:

MediaQueryData.fromWindow(window)

But. in recent versions of Flutter its use is discouraged as it is deprecated; It is important to mention that what is deprecated is the constructor, not the class or API; now, if you want to get the equivalent of the previous function, you should use:

MediaQueryData.fromView(View.of(context));

For example:

screenWidth = MediaQueryData.fromView(View.of(context)).size.width;
screenHeight = MediaQueryData.fromView(View.of(context)).size.height;
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.