debugCheckHasWidgetsLocalizations function Null safety

bool debugCheckHasWidgetsLocalizations(
  1. BuildContext context

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

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.

Does nothing if asserts are disabled. Always returns true.


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