of method

MediaQueryData of (
  1. BuildContext context,
  2. {bool nullOk: false}

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

You can use this function to query the size an orientation of the screen. When that information changes, your widget will be scheduled to be rebuilt, keeping your widget up-to-date.

Typical usage is as follows:

MediaQueryData media = MediaQuery.of(context);

If there is no MediaQuery in scope, then this will throw an exception. To return null if there is no MediaQuery, then pass nullOk: true.

If you use this from a widget (e.g. in its build function), consider calling debugCheckHasMediaQuery.


static MediaQueryData of(BuildContext context, { bool nullOk = false }) {
  assert(context != null);
  assert(nullOk != null);
  final MediaQuery query = context.dependOnInheritedWidgetOfExactType<MediaQuery>();
  if (query != null)
    return query.data;
  if (nullOk)
    return null;
  throw FlutterError.fromParts(<DiagnosticsNode>[
    ErrorSummary('MediaQuery.of() called with a context that does not contain a MediaQuery.'),
      'No MediaQuery ancestor could be found starting from the context that was passed '
      'to MediaQuery.of(). This can happen because you do not have a WidgetsApp or '
      'MaterialApp widget (those widgets introduce a MediaQuery), or it can happen '
      'if the context you use comes from a widget above those widgets.'
    context.describeElement('The context used was')