debugCheckHasOverlay function Null safety

bool debugCheckHasOverlay(
  1. BuildContext context

Asserts that the given context has an Overlay ancestor.

To call this function, use the following pattern, typically in the relevant Widget's build method:


Always place this before any early returns, so that the invariant is checked in all cases. This prevents bugs from hiding until a particular codepath is hit.

This method can be expensive (it walks the element tree).

Does nothing if asserts are disabled. Always returns true.


bool debugCheckHasOverlay(BuildContext context) {
  assert(() {
    if (context.widget is! Overlay && context.findAncestorWidgetOfExactType<Overlay>() == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No Overlay widget found.'),
          '${context.widget.runtimeType} widgets require an Overlay '
          'widget ancestor.\n'
          'An overlay lets widgets float on top of other widget children.',
          'To introduce an Overlay widget, you can either directly '
          'include one, or use a widget that contains an Overlay itself, '
          'such as a Navigator, WidgetApp, MaterialApp, or CupertinoApp.',
        ...context.describeMissingAncestor(expectedAncestorType: Overlay),
    return true;
  return true;