of method Null safety

SliverAnimatedListState of(
  1. BuildContext context
)

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 assert in debug mode and throw an exception in release mode.

This method can be expensive (it walks the element tree).

See also:

Implementation

static SliverAnimatedListState of(BuildContext context) {
  assert(context != null);
  final SliverAnimatedListState? result = context.findAncestorStateOfType<SliverAnimatedListState>();
  assert(() {
    if (result == null) {
      throw FlutterError(
        'SliverAnimatedList.of() called with a context that does not contain a SliverAnimatedList.\n'
        'No SliverAnimatedListState ancestor could be found starting from the '
        'context that was passed to SliverAnimatedListState.of(). This can '
        'happen when the context provided is from the same StatefulWidget that '
        'built the AnimatedList. Please see the SliverAnimatedList documentation '
        'for examples of how to refer to an AnimatedListState object: '
        'https://api.flutter.cn/flutter/widgets/SliverAnimatedListState-class.html\n'
        'The context used was:\n'
        '  $context',
      );
    }
    return true;
  }());
  return result!;
}