PageStorage in Flutter - how does it work?

Below simple example:

home.dart

final pageStorageBucket = PageStorageBucket();

@override
Widget build(BuildContext context) {
  return PageStorage(
    bucket: pageStorageBuket,
    child: Scaffold(
      ...
    ),
  );
}

child.dart

int storedValue;

@override initState() {
  super.initState();
  storedValue = PageStorage.of(context).readState(context, identifier: 'value');
}

void storeValue(int value) {
  PageStorage.of(context).writeState(context, value, identifier: 'value');
}

PageStorage is for tracking the state of widgets that may not always be instantiated, for example the position of parallel list views in a pageable view (like when when you have several tabs each with its own list). For something like sharedpreference, you should probably use sharedpreference itself (via a plugin). See also https://github.com/flutter/flutter/issues/4757 or https://github.com/flutter/flutter/issues/3427.


I'm not sure what PageStorage the class is for, but based on your notes, it sounds like you're looking for key value storage.

There is a bug on file about providing a nice plugin for such: https://github.com/flutter/flutter/issues/4757

Tags:

Flutter