of method Null safety

AnimatedListState of(
  1. BuildContext context
)

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