maybeOf static method

MediaQueryData? maybeOf(
  1. BuildContext context
)

The data from the closest instance of this class that encloses the given context, if any.

Returns null if no MediaQuery is in scope.

When any MediaQueryData information changes, a rebuild is scheduled for the provided context, to ensure that the widget stays up-to-date.

Since it is typical that the widget only requires a subset of properties of the MediaQueryData object, prefer using the more specific methods (for example: MediaQuery.maybeSizeOf and MediaQuery.maybePaddingOf), as those methods will not cause a widget to rebuild when unrelated properties are updated.

Typical usage is as follows:

MediaQueryData? mediaQuery = MediaQuery.maybeOf(context);
if (mediaQuery == null) {
  // Do something else instead.
}

See also:

  • of, which will throw if it doesn't find a MediaQuery ancestor, instead of returning null.
  • maybeSizeOf and other specific methods for retrieving and depending on changes of a specific value.

Implementation

static MediaQueryData? maybeOf(BuildContext context) {
  return _maybeOf(context);
}