prefer-dedicated-media-query-methods
preset: recommended
Warns when MediaQuery.of
or MediaQuery.maybeOf
are used over dedicated methods (ex. sizeOf
).
Using dedicated methods is more performant since it reduces the number of unnecessary rebuilds.
info
Enable this rule only if you have Flutter 3.10 or higher, since dedicated methods are not available in older versions.
Example
❌ Bad:
Widget build(BuildContext context) {
MediaQuery.of(context); // LINT: Prefer dedicated 'MediaQuery' methods instead of general 'of/maybeOf' methods.
MediaQuery.maybeOf(context); // LINT: Prefer dedicated 'MediaQuery' methods instead of general 'of/maybeOf' methods.
...
}
✅ Good:
Widget build(BuildContext context) {
MediaQuery.sizeOf(context);
MediaQuery.highContrastOf(context);
...
}