debugCheckHasHeraLocalizations function

bool debugCheckHasHeraLocalizations(
  1. BuildContext context
)

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

Used by many Hera 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(debugCheckHasHeraLocalizations(context));

This function has the side-effect of establishing an inheritance relationship with the nearest Localizations widget (see BuildContext.dependOnInheritedWidgetOfExactType). This is ok if the caller always also calls Localizations.of or Localizations.localeOf.

Does nothing if asserts are disabled. Always returns true.

Implementation

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