-
Notifications
You must be signed in to change notification settings - Fork 0
Context Extension
Kenny Mochizuki Escalona edited this page Aug 16, 2026
·
1 revision
The LayrzI18nContextExtension provides ergonomic access to translations via the context.i18n getter, eliminating the need to call LayrzI18n.of(context) explicitly.
Gets the LayrzI18n instance from the nearest ancestor in the widget tree.
// Throws FlutterError if LayrzI18n is not in the tree
final i18n = context.i18n;
Text(i18n.t('greeting', {'name': 'Alice'}))This is equivalent to:
final i18n = LayrzI18n.of(context);Gets the LayrzI18n instance from the nearest ancestor, or returns null if not found.
// Returns null if LayrzI18n is not in the tree (safe to use)
final i18n = context.maybeI18n;
if (i18n != null) {
Text(i18n.t('greeting', {'name': 'Alice'}))
} else {
Text('Translations not available')
}This is equivalent to:
final i18n = LayrzI18n.maybeOf(context);If you use context.i18n and LayrzI18n is not in the widget tree, a FlutterError is thrown with the message:
LayrzI18n was used before it was initialized
This indicates that the widget is being rendered outside of the localizationsDelegates scope of your WidgetsApp or MaterialApp. Ensure your widget tree is properly wrapped with the LayrzI18n delegate.
Made with ❤️ by Golden M, Inc.