Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageStorage in Flutter - how does it work?

Tags:

flutter

I've seen PageStorage in the documentation. From what I understand its almost like a sharedpreference alternative from the Android world.

How do I use it, and why does it need a Widget in it's implementation?

like image 227
Faisal Abid Avatar asked Feb 23 '17 14:02

Faisal Abid


3 Answers

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');
}
like image 179
BambinoUA Avatar answered Oct 19 '22 20:10

BambinoUA


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.

like image 29
Ian Hickson Avatar answered Oct 19 '22 21:10

Ian Hickson


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

like image 1
Eric Seidel Avatar answered Oct 19 '22 19:10

Eric Seidel