Make WidgetsLocalizations.of non-nullable (#69055)
This commit is contained in:
parent
291ee94506
commit
95563ff197
@ -259,7 +259,7 @@ abstract class CupertinoLocalizations {
|
||||
/// CupertinoLocalizations.of(context).anteMeridiemAbbreviation;
|
||||
/// ```
|
||||
static CupertinoLocalizations of(BuildContext context) {
|
||||
debugCheckHasCupertinoLocalizations(context);
|
||||
assert(debugCheckHasCupertinoLocalizations(context));
|
||||
return Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations)!;
|
||||
}
|
||||
}
|
||||
|
@ -506,7 +506,7 @@ abstract class MaterialLocalizations {
|
||||
/// tooltip: MaterialLocalizations.of(context).backButtonTooltip,
|
||||
/// ```
|
||||
static MaterialLocalizations of(BuildContext context) {
|
||||
debugCheckHasMaterialLocalizations(context);
|
||||
assert(debugCheckHasMaterialLocalizations(context));
|
||||
return Localizations.of<MaterialLocalizations>(context, MaterialLocalizations)!;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'basic.dart';
|
||||
import 'framework.dart';
|
||||
import 'localizations.dart';
|
||||
import 'media_query.dart';
|
||||
import 'table.dart';
|
||||
|
||||
@ -321,6 +322,44 @@ void debugWidgetBuilderValue(Widget widget, Widget? built) {
|
||||
}());
|
||||
}
|
||||
|
||||
/// Asserts that the given context has a [Localizations] ancestor that contains
|
||||
/// a [WidgetsLocalizations] delegate.
|
||||
///
|
||||
/// To call this function, use the following pattern, typically in the
|
||||
/// relevant Widget's build method:
|
||||
///
|
||||
/// ```dart
|
||||
/// assert(debugCheckHasWidgetsLocalizations(context));
|
||||
/// ```
|
||||
///
|
||||
/// Does nothing if asserts are disabled. Always returns true.
|
||||
bool debugCheckHasWidgetsLocalizations(BuildContext context) {
|
||||
assert(() {
|
||||
if (Localizations.of<WidgetsLocalizations>(context, WidgetsLocalizations) == null) {
|
||||
throw FlutterError.fromParts(<DiagnosticsNode>[
|
||||
ErrorSummary('No WidgetsLocalizations found.'),
|
||||
ErrorDescription(
|
||||
'${context.widget.runtimeType} widgets require WidgetsLocalizations '
|
||||
'to be provided by a Localizations widget ancestor.'
|
||||
),
|
||||
ErrorDescription(
|
||||
'The widgets library uses Localizations to generate messages, '
|
||||
'labels, and abbreviations.'
|
||||
),
|
||||
ErrorHint(
|
||||
'To introduce a WidgetsLocalizations, either use a '
|
||||
'WidgetsApp at the root of your application to include them '
|
||||
'automatically, or add a Localization widget with a '
|
||||
'WidgetsLocalizations delegate.'
|
||||
),
|
||||
...context.describeMissingAncestor(expectedAncestorType: WidgetsLocalizations)
|
||||
]);
|
||||
}
|
||||
return true;
|
||||
}());
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Returns true if none of the widget library debug variables have been changed.
|
||||
///
|
||||
/// This function is used by the test framework to ensure that debug variables
|
||||
|
@ -10,6 +10,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'basic.dart';
|
||||
import 'binding.dart';
|
||||
import 'container.dart';
|
||||
import 'debug.dart';
|
||||
import 'framework.dart';
|
||||
|
||||
// Examples can assume:
|
||||
@ -155,7 +156,7 @@ abstract class WidgetsLocalizations {
|
||||
/// that encloses the given context.
|
||||
///
|
||||
/// This method is just a convenient shorthand for:
|
||||
/// `Localizations.of<WidgetsLocalizations>(context, WidgetsLocalizations)`.
|
||||
/// `Localizations.of<WidgetsLocalizations>(context, WidgetsLocalizations)!`.
|
||||
///
|
||||
/// References to the localized resources defined by this class are typically
|
||||
/// written in terms of this method. For example:
|
||||
@ -163,8 +164,9 @@ abstract class WidgetsLocalizations {
|
||||
/// ```dart
|
||||
/// textDirection: WidgetsLocalizations.of(context).textDirection,
|
||||
/// ```
|
||||
static WidgetsLocalizations? of(BuildContext context) {
|
||||
return Localizations.of<WidgetsLocalizations>(context, WidgetsLocalizations);
|
||||
static WidgetsLocalizations of(BuildContext context) {
|
||||
assert(debugCheckHasWidgetsLocalizations(context));
|
||||
return Localizations.of<WidgetsLocalizations>(context, WidgetsLocalizations)!;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,6 +215,33 @@ void main() {
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('debugCheckHasWidgetsLocalizations throws', (WidgetTester tester) async {
|
||||
final GlobalKey noLocalizationsAvailable = GlobalKey();
|
||||
final GlobalKey localizationsAvailable = GlobalKey();
|
||||
|
||||
await tester.pumpWidget(
|
||||
Container(
|
||||
key: noLocalizationsAvailable,
|
||||
child: WidgetsApp(
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return Container(
|
||||
key: localizationsAvailable,
|
||||
);
|
||||
},
|
||||
color: const Color(0xFF4CAF50),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(() => debugCheckHasWidgetsLocalizations(noLocalizationsAvailable.currentContext!), throwsA(isAssertionError.having(
|
||||
(AssertionError e) => e.message,
|
||||
'message',
|
||||
contains('No WidgetsLocalizations found'),
|
||||
)));
|
||||
|
||||
expect(debugCheckHasWidgetsLocalizations(localizationsAvailable.currentContext!), isTrue);
|
||||
});
|
||||
|
||||
test('debugAssertAllWidgetVarsUnset', () {
|
||||
debugHighlightDeprecatedWidgets = true;
|
||||
late FlutterError error;
|
||||
|
Loading…
x
Reference in New Issue
Block a user