Skip to content

media_query_of_for_one_property

Reports MediaQuery.of(context) read for one property that has a method of its own. Call that method, such as MediaQuery.sizeOf(context), instead.

Tier
advisory
Scope
Flutter
Lowest preset
recommended
Fix
calls the method of the property

A read of one property of MediaQuery.of(context) where MediaQuery has a method of its own for it:

  • MediaQuery.of(context).size, for MediaQuery.sizeOf(context);
  • MediaQuery.maybeOf(context)?.size, for MediaQuery.maybeSizeOf(context).

The methods are looked up on MediaQuery as your Flutter version declares them. A media query kept in a variable first and read for one property is not reported.

The fix calls the property’s method, keeping an import prefix and the argument.

The rule reports nothing in a package that does not use Flutter.

of makes the widget depend on the whole media query, so it is rebuilt whenever any part of it changes, for example on each frame while the keyboard slides in. The property’s method makes it depend on that property alone.

Violating
final width = MediaQuery.of(context).size.width;
Corrected
final width = MediaQuery.sizeOf(context).width;

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why this place is fine.
// ignore: media_query_of_for_one_property

// ignore_for_file: assay/media_query_of_for_one_property silences it in a whole file, and the prefixed // ignore: assay/media_query_of_for_one_property works too. See Suppressions.