Sub topic 9
PageStorageKey: Remembering Scroll Position
Prerequisites and working model
This chapter assumes that you can write StatelessWidget and StatefulWidget classes, use setState, and build lists with ListView.
A PageStorageKey identifies a scrollable inside a PageStorage bucket. A bucket is a storage object attached to part of the widget tree. Flutter's scrollable widgets use that bucket to save their scroll offset, which is the current position in the list.
This is different from preserving a row's State. A key such as ValueKey helps Flutter match an item with its existing Element. A PageStorageKey stores page-related values, such as a scroll position, so Flutter can restore them later.
How PageStorage saves an offset
A ListView normally uses a ScrollController. If the controller's keepScrollOffset setting is enabled, the scrollable writes its offset to the nearest PageStorage after scrolling. When the scrollable is created again, it reads the saved value.
A PageStorageKey gives the scrollable a stable address in that storage:
The bucket must still exist for restoration to work. A PageStorageKey is not a database key and does not survive an application restart.
Add a stable key to a list
The following complete example places a keyed list in a Material app:
import 'package:flutter/material.dart';
void main() {
runApp(const DemoApp());
}
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Feed')),
body: const Feed(),
),
);
}
}
class Feed extends StatelessWidget {
const Feed({super.key});
@override
Widget build(BuildContext context) {
return ListView.builder(
key: const PageStorageKey<String>('feed'),
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
);
}
}Scroll down, remove Feed from the tree, and later build it again under the same PageStorage bucket. Flutter can restore the saved offset. This commonly happens when switching between tabs or when a route remains in a navigator stack while another route is displayed.
The key value must identify this scrollable among its siblings in the same bucket. If two independent lists both use 'feed', their saved values can conflict. Use different values such as 'inbox' and 'archive'.
Common failure modes
- The key changes: A key based on a random number, current time, or changing list data points to a new storage entry. Use a constant or otherwise stable value.
- The bucket is gone: If the ancestor
PageStorageis disposed, its saved values disappear with it. - The list uses a new identity: Giving two screens the same key does not merge their storage unless they share the relevant
PageStoragebucket. - The scroll controller disables storage: A controller created with
keepScrollOffset: falsewill not save its offset throughPageStorage. - The wrong tool is chosen:
PageStorageKeyrestores page values. It does not keep every childStatealive while a lazy list removes off-screen children.
A PageStorageKey works well when the user expects a page or tab to return to its previous reading position. Use AutomaticKeepAliveClientMixin when a lazy parent should retain a child State while it is off screen. Lift state into a parent when the data is important independently of a particular page or scrollable.
Exercise
What should you change if two lists in the same page both use const PageStorageKey<String>('items')?
Show answer
Give them distinct, stable values, such as 'recent-items' and 'saved-items', so their scroll positions use separate storage entries.