flutter_hooks icon indicating copy to clipboard operation
flutter_hooks copied to clipboard

Interoperability with state restoration

Open foodchaining opened this issue 5 years ago • 9 comments
trafficstars

Hi, There is a state restoration facility upcoming to flutter. Will flutter_hooks be compatible with it?

foodchaining avatar Aug 06 '20 12:08 foodchaining

For now, you can use StatefulHookWidget and the official API for restorable state

A hook support may need some PR

rrousselGit avatar Aug 06 '20 16:08 rrousselGit

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.

blaugold avatar Aug 14 '20 23:08 blaugold

I experimented with this a bit and came up with https://github.com/knaeckeKami/state_restoration_hook

knaeckeKami avatar Jun 14 '23 15:06 knaeckeKami

@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.

rrousselGit avatar Jun 14 '23 15:06 rrousselGit

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.

knaeckeKami avatar Jun 14 '23 15:06 knaeckeKami

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 (*/]);

   ...    
});

rrousselGit avatar Jun 14 '23 15:06 rrousselGit

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.

knaeckeKami avatar Jun 14 '23 16:06 knaeckeKami

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.

rrousselGit avatar Jun 14 '23 16:06 rrousselGit

Thanks! I'll work on refining this later, might take a couple of weeks though.

knaeckeKami avatar Jun 14 '23 16:06 knaeckeKami