Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interoperability with state restoration #164

Open
foodchaining opened this issue Aug 6, 2020 · 9 comments
Open

Interoperability with state restoration #164

foodchaining opened this issue Aug 6, 2020 · 9 comments
Assignees
Labels
enhancement New feature or request

Comments

@foodchaining
Copy link

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

@rrousselGit
Copy link
Owner

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

A hook support may need some PR

@rrousselGit rrousselGit added the enhancement New feature or request label Aug 8, 2020
@blaugold
Copy link

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.

@rrousselGit rrousselGit self-assigned this May 10, 2023
@knaeckeKami
Copy link

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

@rrousselGit
Copy link
Owner

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

@knaeckeKami
Copy link

knaeckeKami commented Jun 14, 2023

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.

@rrousselGit
Copy link
Owner

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

   ...    
});

@knaeckeKami
Copy link

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.

@rrousselGit
Copy link
Owner

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.

@knaeckeKami
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants