Unverified Commit 8f10a262 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Remove vestigal nullOk parameter from Localizations.localeOf (#74657)

This removes the nullOk parameter on the Localizations.localeOf method. This was already changed to have a maybeLocaleOf method, but the nullOk parameter wasn't removed because some third party tests failed. Those have been updated, so this can go in now.
parent c17be719
...@@ -387,7 +387,7 @@ class Localizations extends StatefulWidget { ...@@ -387,7 +387,7 @@ class Localizations extends StatefulWidget {
mergedDelegates.insertAll(0, delegates); mergedDelegates.insertAll(0, delegates);
return Localizations( return Localizations(
key: key, key: key,
locale: locale ?? Localizations.localeOf(context)!, locale: locale ?? Localizations.localeOf(context),
delegates: mergedDelegates, delegates: mergedDelegates,
child: child, child: child,
); );
...@@ -409,13 +409,10 @@ class Localizations extends StatefulWidget { ...@@ -409,13 +409,10 @@ class Localizations extends StatefulWidget {
/// corresponds to [BuildContext] `context`. /// corresponds to [BuildContext] `context`.
/// ///
/// If no [Localizations] widget is in scope then the [Localizations.localeOf] /// If no [Localizations] widget is in scope then the [Localizations.localeOf]
/// method will throw an exception, unless the `nullOk` argument is set to /// method will throw an exception.
/// true, in which case it returns null. static Locale localeOf(BuildContext context) {
static Locale? localeOf(BuildContext context, { bool nullOk = false }) {
assert(context != null); assert(context != null);
final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>(); final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>();
if (nullOk && scope == null)
return null;
assert(() { assert(() {
if (scope == null) { if (scope == null) {
throw FlutterError( throw FlutterError(
...@@ -424,21 +421,21 @@ class Localizations extends StatefulWidget { ...@@ -424,21 +421,21 @@ class Localizations extends StatefulWidget {
'be that of a widget that is a descendant of a Localizations widget.' 'be that of a widget that is a descendant of a Localizations widget.'
); );
} }
if (!nullOk && scope.localizationsState.locale == null) { if (scope.localizationsState.locale == null) {
throw FlutterError( throw FlutterError(
'Localizations.localeOf found a Localizations widget that had a unexpected null locale.\n' 'Localizations.localeOf found a Localizations widget that had a unexpected null locale.\n'
); );
} }
return true; return true;
}()); }());
return scope!.localizationsState.locale; return scope!.localizationsState.locale!;
} }
/// The locale of the Localizations widget for the widget tree that /// The locale of the Localizations widget for the widget tree that
/// corresponds to [BuildContext] `context`. /// corresponds to [BuildContext] `context`.
/// ///
/// If no [Localizations] widget is in scope then this function will return /// If no [Localizations] widget is in scope then this function will return
/// null. Equivalent to calling [localeOf] with `nullOk` set to true. /// null.
static Locale? maybeLocaleOf(BuildContext context) { static Locale? maybeLocaleOf(BuildContext context) {
assert(context != null); assert(context != null);
final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>(); final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>();
......
...@@ -575,7 +575,7 @@ void main() { ...@@ -575,7 +575,7 @@ void main() {
context: context, context: context,
child: Builder( child: Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
final TextDirection direction = WidgetsLocalizations.of(context).textDirection; final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
return Text('$locale $direction'); return Text('$locale $direction');
}, },
...@@ -616,7 +616,7 @@ void main() { ...@@ -616,7 +616,7 @@ void main() {
], ],
child: Builder( child: Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
final TextDirection direction = WidgetsLocalizations.of(context).textDirection; final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
return Text('$locale $direction'); return Text('$locale $direction');
}, },
...@@ -652,7 +652,7 @@ void main() { ...@@ -652,7 +652,7 @@ void main() {
const OnlyRTLDefaultWidgetsLocalizationsDelegate(), const OnlyRTLDefaultWidgetsLocalizationsDelegate(),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
final TextDirection direction = WidgetsLocalizations.of(context).textDirection; final TextDirection direction = WidgetsLocalizations.of(context).textDirection;
return Text('$locale $direction'); return Text('$locale $direction');
}, },
...@@ -760,7 +760,7 @@ void main() { ...@@ -760,7 +760,7 @@ void main() {
Locale('ja'), Locale('ja'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -783,7 +783,7 @@ void main() { ...@@ -783,7 +783,7 @@ void main() {
Locale('zh', 'TW'), Locale('zh', 'TW'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -815,7 +815,7 @@ void main() { ...@@ -815,7 +815,7 @@ void main() {
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'), Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -956,7 +956,7 @@ void main() { ...@@ -956,7 +956,7 @@ void main() {
Locale.fromSubtags(languageCode: 'zh'), Locale.fromSubtags(languageCode: 'zh'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1096,7 +1096,7 @@ void main() { ...@@ -1096,7 +1096,7 @@ void main() {
Locale('it', 'IT'), Locale('it', 'IT'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1119,7 +1119,7 @@ void main() { ...@@ -1119,7 +1119,7 @@ void main() {
Locale('it', 'IT'), Locale('it', 'IT'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1152,7 +1152,7 @@ void main() { ...@@ -1152,7 +1152,7 @@ void main() {
Locale('pt', 'PT'), Locale('pt', 'PT'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1219,7 +1219,7 @@ void main() { ...@@ -1219,7 +1219,7 @@ void main() {
Locale('de', 'DE'), Locale('de', 'DE'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1283,7 +1283,7 @@ void main() { ...@@ -1283,7 +1283,7 @@ void main() {
Locale('vi'), Locale('vi'),
], ],
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
...@@ -1435,7 +1435,7 @@ void main() { ...@@ -1435,7 +1435,7 @@ void main() {
return const Locale('en', 'US'); return const Locale('en', 'US');
}, },
buildContent: (BuildContext context) { buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context)!; final Locale locale = Localizations.localeOf(context);
return Text('$locale'); return Text('$locale');
}, },
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment