flutter_hooks
flutter_hooks copied to clipboard
Interoperability with state restoration
Hi,
There is a state restoration facility upcoming to flutter. Will flutter_hooks be compatible with it?
For now, you can use StatefulHookWidget and the official API for restorable state
A hook support may need some PR
Hey @rrousselGit and @foodchaining,
I've created a PR (#167) to add support for managing RestorableProperty with a hook. Would love to get some feedback whether that's a good direction.
I experimented with this a bit and came up with https://github.com/knaeckeKami/state_restoration_hook
@knaeckeKami In your example, is there any way to avoid that addPostFrameCallback in the restoreState?
One core problem behind this feature is, we want the initial build to already be restored.
Are you talking about the addPostFrameCallback in the example?
This is only needed to show the Snackbar in case the state was restored, the first build would already have the correct restored state.
The first build would already have the restored state, the restored state is available immediately after the registerForRestoration call.
Oh I see.
Still, I find it a bit verbose.
Personally, I would prefer if your example could be written as:
class MyHomePage extends HookWidget {
@override
Widget build(BuildContext context) {
useRestorationBucket('MyHomePage');
final counter = useRestorableInt(0, 'counter');
// show the modal like in your example
useEffect(() {
if (counter.value != 0) addPostFrameCallback(<show snackbar>);
}, [/* only do this check on initial build (*/]);
...
});
Yes, it's definitely verbose, it's basically a full port of the RestorationMixin API. In the useRestoration callback you can do things like unregister previously registered properties or change the restoration id using the didUpdateRestorationId method.
Most use cases probably don't need that, so it might make sense to trim the API down.
It's just a first draft of what state restoration using hooks might look like, hence the experimental status, I'm happy for feedback on what could be improved in the API.
I'd be happy to help you iterate over the API if you want.
Feel free to reach out either publicly here or privately on discord if you have questions or want me to look at specific things.
Thanks! I'll work on refining this later, might take a couple of weeks though.