maybeOf static method

PlatformQueryData? maybeOf(
  1. BuildContext context
)

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

Use this function if you want to allow situations where no PlatformQuery is in scope. Prefer using PlatformQuery.of in situations where a device query is always expected to exist.

If there is no PlatformQuery in scope, then this function will return null.

You can use this function to query the size and orientation of the screen, as well as other device parameters (see PlatformQueryData for more examples). When that information changes, your widget will be scheduled to be rebuilt, keeping your widget up-to-date.

Typical usage is as follows:

PlatformQueryData? platformQuery = PlatformQuery.maybeOf(context);
if (platformQuery == null) {
  // Do something else instead.
}

See also:

  • of, which will throw if it doesn't find a PlatformQuery ancestor, instead of returning null.

Implementation

static PlatformQueryData? maybeOf(BuildContext context) {
  return context.dependOnInheritedWidgetOfExactType<PlatformQuery>()?.data;
}