of method

MediaQueryData of (BuildContext context, { 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.

Implementation

static MediaQueryData of(BuildContext context, { bool nullOk = false }) {
  assert(context != null);
  assert(nullOk != null);
  final MediaQuery query = context.inheritFromWidgetOfExactType(MediaQuery);
  if (query != null)
    return query.data;
  if (nullOk)
    return null;
  throw FlutterError(
    'MediaQuery.of() called with a context that does not contain a MediaQuery.\n'
    '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.\n'
    'The context used was:\n'
    '  $context'
  );
}