When our ListView uses the Builder method to render the child component lazily, if the child component is a StatefulWidget, the state of the child component will be lost once the ListView is refreshed. For example, the subcomponent of my project uses a text input field. After the state is lost, the original input content of the text input field disappears. Solution is very simple, in a child component of the State class, using mixed with keyword AutomaticKeepAliveClientMixin classes, and rewrite wantKeepAlive to true.

class _YourState with AutomaticKeepAliveClientMixin {

@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;

}
Copy the code