debugCheckHasScaffoldMessenger function Null safety

bool debugCheckHasScaffoldMessenger (
  1. BuildContext context
)

Asserts that the given context has a ScaffoldMessenger ancestor.

Used by various widgets to make sure that they are only used in an appropriate context.

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

assert(debugCheckHasScaffoldMessenger(context));

Does nothing if asserts are disabled. Always returns true.

Implementation

bool debugCheckHasScaffoldMessenger(BuildContext context) {
  assert(() {
    if (context.findAncestorWidgetOfExactType<ScaffoldMessenger>() == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No ScaffoldMessenger widget found.'),
        ErrorDescription('${context.widget.runtimeType} widgets require a ScaffoldMessenger widget ancestor.'),
        ...context.describeMissingAncestor(expectedAncestorType: ScaffoldMessenger),
        ErrorHint(
          'Typically, the ScaffoldMessenger widget is introduced by the MaterialApp '
          'at the top of your application widget tree.'
        )
      ]);
    }
    return true;
  }());
  return true;
}