debugCheckHasMaterialLocalizations function Null safety

bool debugCheckHasMaterialLocalizations(
  1. BuildContext context

Asserts that the given context has a Localizations ancestor that contains a MaterialLocalizations delegate.

Used by many material design widgets to make sure that they are only used in contexts where they have access to localizations.

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


Does nothing if asserts are disabled. Always returns true.


bool debugCheckHasMaterialLocalizations(BuildContext context) {
  assert(() {
    if (Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No MaterialLocalizations found.'),
          '${context.widget.runtimeType} widgets require MaterialLocalizations '
          'to be provided by a Localizations widget ancestor.',
          'The material library uses Localizations to generate messages, '
          'labels, and abbreviations.',
          'To introduce a MaterialLocalizations, either use a '
          'MaterialApp at the root of your application to include them '
          'automatically, or add a Localization widget with a '
          'MaterialLocalizations delegate.',
        ...context.describeMissingAncestor(expectedAncestorType: MaterialLocalizations),
    return true;
  return true;