debugCheckHasFluentLocalizations function

bool debugCheckHasFluentLocalizations(
  1. BuildContext context
)

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

Used by many fluent 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:

assert(debugCheckHasFluentLocalizations(context));

Does nothing if asserts are disabled. Always returns true.

Implementation

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