Unverified Commit e5f7726c authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Remove nullOk parameter from AnimatedList.of and SliverAnimatedList.of (#68925)

This removes the nullOk parameter from AnimatedList.of and SliverAnimatedList.of, and creates maybeOf equivalents for each. The of methods now return non-nullable values, and the maybeOf equivalents return nullable values.
parent 85a25256
...@@ -382,26 +382,53 @@ class AnimatedList extends StatefulWidget { ...@@ -382,26 +382,53 @@ class AnimatedList extends StatefulWidget {
/// This method is typically used by [AnimatedList] item widgets that insert /// This method is typically used by [AnimatedList] item widgets that insert
/// or remove items in response to user input. /// or remove items in response to user input.
/// ///
/// ```dart /// If no [AnimatedList] surrounds the context given, then this function will
/// AnimatedListState animatedList = AnimatedList.of(context); /// assert in debug mode and throw an exception in release mode.
/// ``` ///
static AnimatedListState? of(BuildContext context, { bool nullOk = false }) { /// See also:
///
/// * [maybeOf], a similar function that will return null if no
/// [AnimatedList] ancestor is found.
static AnimatedListState of(BuildContext context) {
assert(context != null); assert(context != null);
assert(nullOk != null);
final AnimatedListState? result = context.findAncestorStateOfType<AnimatedListState>(); final AnimatedListState? result = context.findAncestorStateOfType<AnimatedListState>();
if (nullOk || result != null) assert((){
return result; if (result == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('AnimatedList.of() called with a context that does not contain an AnimatedList.'), ErrorSummary(
ErrorDescription('No AnimatedList ancestor could be found starting from the context that was passed to AnimatedList.of().'), 'AnimatedList.of() called with a context that does not contain an AnimatedList.'),
ErrorHint( ErrorDescription(
'This can happen when the context provided is from the same StatefulWidget that ' 'No AnimatedList ancestor could be found starting from the context that was passed to AnimatedList.of().'),
'built the AnimatedList. Please see the AnimatedList documentation for examples ' ErrorHint(
'of how to refer to an AnimatedListState object:' 'This can happen when the context provided is from the same StatefulWidget that '
' https://api.flutter.dev/flutter/widgets/AnimatedListState-class.html' 'built the AnimatedList. Please see the AnimatedList documentation for examples '
), 'of how to refer to an AnimatedListState object:'
context.describeElement('The context used was') ' https://api.flutter.dev/flutter/widgets/AnimatedListState-class.html'
]); ),
context.describeElement('The context used was')
]);
}
return true;
}());
return result!;
}
/// The state from the closest instance of this class that encloses the given
/// context.
///
/// This method is typically used by [AnimatedList] item widgets that insert
/// or remove items in response to user input.
///
/// If no [AnimatedList] surrounds the context given, then this function will
/// return null.
///
/// See also:
///
/// * [of], a similar function that will throw if no [AnimatedList] ancestor
/// is found.
static AnimatedListState? maybeOf(BuildContext context) {
assert(context != null);
return context.findAncestorStateOfType<AnimatedListState>();
} }
@override @override
...@@ -770,25 +797,50 @@ class SliverAnimatedList extends StatefulWidget { ...@@ -770,25 +797,50 @@ class SliverAnimatedList extends StatefulWidget {
/// This method is typically used by [SliverAnimatedList] item widgets that /// This method is typically used by [SliverAnimatedList] item widgets that
/// insert or remove items in response to user input. /// insert or remove items in response to user input.
/// ///
/// ```dart /// If no [SliverAnimatedList] surrounds the context given, then this function
/// SliverAnimatedListState animatedList = SliverAnimatedList.of(context); /// will assert in debug mode and throw an exception in release mode.
/// ``` ///
static SliverAnimatedListState? of(BuildContext context, {bool nullOk = false}) { /// See also:
///
/// * [maybeOf], a similar function that will return null if no
/// [SliverAnimatedList] ancestor is found.
static SliverAnimatedListState of(BuildContext context) {
assert(context != null); assert(context != null);
assert(nullOk != null);
final SliverAnimatedListState? result = context.findAncestorStateOfType<SliverAnimatedListState>(); final SliverAnimatedListState? result = context.findAncestorStateOfType<SliverAnimatedListState>();
if (nullOk || result != null) assert((){
return result; if (result == null) {
throw FlutterError( throw FlutterError(
'SliverAnimatedList.of() called with a context that does not contain a SliverAnimatedList.\n' 'SliverAnimatedList.of() called with a context that does not contain a SliverAnimatedList.\n'
'No SliverAnimatedListState ancestor could be found starting from the ' 'No SliverAnimatedListState ancestor could be found starting from the '
'context that was passed to SliverAnimatedListState.of(). This can ' 'context that was passed to SliverAnimatedListState.of(). This can '
'happen when the context provided is from the same StatefulWidget that ' 'happen when the context provided is from the same StatefulWidget that '
'built the AnimatedList. Please see the SliverAnimatedList documentation ' 'built the AnimatedList. Please see the SliverAnimatedList documentation '
'for examples of how to refer to an AnimatedListState object: ' 'for examples of how to refer to an AnimatedListState object: '
'https://docs.flutter.io/flutter/widgets/SliverAnimatedListState-class.html \n' 'https://docs.flutter.io/flutter/widgets/SliverAnimatedListState-class.html\n'
'The context used was:\n' 'The context used was:\n'
' $context'); ' $context');
}
return true;
}());
return result!;
}
/// The state from the closest instance of this class that encloses the given
/// context.
///
/// This method is typically used by [SliverAnimatedList] item widgets that
/// insert or remove items in response to user input.
///
/// If no [SliverAnimatedList] surrounds the context given, then this function
/// will return null.
///
/// See also:
///
/// * [of], a similar function that will throw if no [SliverAnimatedList]
/// ancestor is found.
static SliverAnimatedListState? maybeOf(BuildContext context) {
assert(context != null);
return context.findAncestorStateOfType<SliverAnimatedListState>();
} }
} }
......
...@@ -321,11 +321,12 @@ void main() { ...@@ -321,11 +321,12 @@ void main() {
}); });
}); });
testWidgets('AnimatedList.of() called with a context that does not contain AnimatedList', testWidgets('AnimatedList.of() and maybeOf called with a context that does not contain AnimatedList',
(WidgetTester tester) async { (WidgetTester tester) async {
final GlobalKey key = GlobalKey(); final GlobalKey key = GlobalKey();
await tester.pumpWidget(Container(key: key)); await tester.pumpWidget(Container(key: key));
late FlutterError error; late FlutterError error;
expect(AnimatedList.maybeOf(key.currentContext!), isNull);
try { try {
AnimatedList.of(key.currentContext!); AnimatedList.of(key.currentContext!);
} on FlutterError catch (e) { } on FlutterError catch (e) {
......
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