debugCheckHasMercuryLocalizations function
Asserts that the given context has a Localizations ancestor that contains a MercuryLocalizations delegate.
Used by many Mercury 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(debugCheckHasMercuryLocalizations(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 debugCheckHasMercuryLocalizations(BuildContext context) {
assert(() {
if (Localizations.of<MercuryLocalizations>(context, MercuryLocalizations) == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('No MercuryLocalizations found.'),
ErrorDescription(
'${context.widget.runtimeType} widgets require MercuryLocalizations '
'to be provided by a Localizations widget ancestor.',
),
ErrorDescription(
'The Mercury library uses Localizations to generate messages, '
'labels, and abbreviations.',
),
ErrorHint(
'To introduce a MercuryLocalizations, either use a '
'MaterialApp or CupertinoApp with localizationsDelegates: '
'[DefaultMercuryLocalizations.delegate] at the root of your '
'application, or add a Localization widget with a '
'MercuryLocalizations delegate.',
),
...context.describeMissingAncestor(expectedAncestorType: MercuryLocalizations),
]);
}
return true;
}());
return true;
}