Check for empty and null preferredLocales before passing to 'localeResolutionCallback'. (#24481)

This commit is contained in:
Noor Dawod
2018-11-19 23:17:47 +01:00
committed by Gary Qian
parent ec4f22c17b
commit 9d2037e9a8
2 changed files with 41 additions and 1 deletions

View File

@@ -810,7 +810,10 @@ class _WidgetsAppState extends State<WidgetsApp> implements WidgetsBindingObserv
}
// localeListResolutionCallback failed, falling back to localeResolutionCallback.
if (widget.localeResolutionCallback != null) {
final Locale locale = widget.localeResolutionCallback(preferredLocales.first, widget.supportedLocales);
final Locale locale = widget.localeResolutionCallback(
preferredLocales != null && preferredLocales.isNotEmpty ? preferredLocales.first : null,
widget.supportedLocales,
);
if (locale != null)
return locale;
}

View File

@@ -1414,4 +1414,41 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('en_CA'), findsOneWidget);
});
testWidgets('WidgetsApp invalid preferredLocales', (WidgetTester tester) async {
await tester.pumpWidget(
buildFrame(
supportedLocales: const <Locale>[
Locale('zh', 'CN'),
Locale('en', 'CA'),
Locale('en', 'US'),
Locale('en', 'AU'),
Locale('de', 'DE'),
],
localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales) {
if (locale == null)
return const Locale('und', 'US');
return const Locale('en', 'US');
},
buildContent: (BuildContext context) {
final Locale locale = Localizations.localeOf(context);
return Text('$locale');
}
)
);
await tester.binding.setLocales(const <Locale>[
Locale.fromSubtags(languageCode: 'en', countryCode: 'US'),]
);
await tester.pumpAndSettle();
expect(find.text('en_US'), findsOneWidget);
await tester.binding.setLocales(null);
await tester.pumpAndSettle();
expect(find.text('und_US'), findsOneWidget);
await tester.binding.setLocales(const <Locale>[]);
await tester.pumpAndSettle();
expect(find.text('und_US'), findsOneWidget);
});
}