From 855a1481b24a2ac55f68a6e7754062ce3b868b41 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 10 Jul 2021 00:19:20 +1000 Subject: [PATCH] I18N fallback for null context --- lib/l10.dart | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/l10.dart b/lib/l10.dart index 571bd8f0..04b70f40 100644 --- a/lib/l10.dart +++ b/lib/l10.dart @@ -1,9 +1,22 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:one_context/one_context.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations_en.dart'; +import 'package:one_context/one_context.dart'; +import 'package:flutter/material.dart'; // Shortcut function to reduce boilerplate! I18N L10() { - return I18N.of(OneContext().context)!; + BuildContext? _ctx = OneContext().context; + + if (_ctx != null) { + I18N? i18n = I18N.of(_ctx); + + if (i18n != null) { + return i18n; + } + } + + // Fallback for "null" context + return I18NEn(); } \ No newline at end of file