AutomaticKeepAlive class

Allows subtrees to request to be kept alive in lazy lists.

This widget is like KeepAlive but instead of being explicitly configured, it listens to KeepAliveNotification messages from the child and other descendants.

The subtree is kept alive whenever there is one or more descendant that has sent a KeepAliveNotification and not yet triggered its KeepAliveNotification.handle.

To send these notifications, consider using AutomaticKeepAliveClientMixin.

The SliverChildBuilderDelegate and SliverChildListDelegate delegates, used with SliverList and SliverGrid, as well as the scroll view counterparts ListView and GridView, have an addAutomaticKeepAlives feature, which is enabled by default. This feature inserts AutomaticKeepAlive widgets around each child, which in turn configure KeepAlive widgets in response to KeepAliveNotifications.

The same addAutomaticKeepAlives feature is supported by TwoDimensionalChildBuilderDelegate and TwoDimensionalChildListDelegate.

This sample demonstrates how to use the AutomaticKeepAlive widget in combination with the AutomaticKeepAliveClientMixin to selectively preserve the state of individual items in a scrollable list.

Normally, widgets in a lazily built list like ListView.builder are disposed of when they leave the visible area to maintain performance. This means that any state inside a StatefulWidget would be lost unless explicitly preserved.

In this example, each list item is a StatefulWidget that includes a counter and an increment button. To preserve the state of selected items (based on their index), the AutomaticKeepAlive widget and AutomaticKeepAliveClientMixin are used:

  • The wantKeepAlive getter in the item’s state class returns true for even-indexed items, indicating that their state should be preserved.
  • For odd-indexed items, wantKeepAlive returns false, so their state is not preserved when scrolled out of view.
link

To create a local project with this code sample, run:
flutter create --sample=widgets.AutomaticKeepAlive.1 mysample

See also:

Inheritance

Constructors

AutomaticKeepAlive.new({Key? key, required Widget child})
Creates a widget that listens to KeepAliveNotifications and maintains a KeepAlive widget appropriately.
const

Properties

child Widget
The widget below this widget in the tree.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<AutomaticKeepAlive>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited