-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Add support for using RestorableProperty with hook #167
Conversation
a1399b3
to
018fb8e
Compare
018fb8e
to
80e8ac0
Compare
restorationId: 'root', | ||
// This widget provides hooks access to a `RestorationBucket`. It is a | ||
// direct replacement for `RestorationScope` from the framework. | ||
child: HookRestorationScope( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need such widget. Relying on an InheritedWidget to work around the RestorationMixin
could be a significant source of problems.
I would expect people to easily forget to wrap a custom HookWidget
using useRestorableProperty
into a HookRestorationScope
– and in fact, they shouldn't have to.
useRestorableProperty
should be able to handle everything by itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it's not optimal. The reason I implemented it this way is that the implementation of RestorableProperty
is tied to RestorationMixin
. I have not looked into if it would be possible to use RestorableProperty
without RestorationMixin
but that doesn't seem like a good idea. If users can't use RestorableProperty
, they also can't use all the subclasses which would mean quite a large part of the user facing restoration API would need to be replaced somehow. I'll take a second look at how to get around HookRestorationScope
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is, we can't reasonably expose the RestorationMixin
through an InheritedWidget
That could cause major issues, as the mixin could suddenly be associated with multiple HookWidget
)); | ||
|
||
if (listen) { | ||
useListenable(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditionally calling hooks is anti-pattern. The listen
flag probably shouldn't be there at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed most users will want to listen to the property changes but thinking about it I'm not so sure. I'll remove the option and document that properties need to be listened to explicitly for example with useListenable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant "always listen" instead, rather than optionally listen.
If we want to support optional listening, this should probably be in a different hook rather than as a flag
I think there are a few paths we can explore for the In the end, the only thing we care about here is passing the So we have a few options:
class Example extends HookWidget {
@override
String get restorationId => 'foo';
}
class Example extends HookWidget {
@override
Widget build(context) {
useRestoration('foo');
final counter = useRestorableInt(0);
}
} I also believe that we shouldn't have a single final count = useRestorableInt(0, 'count');
final str = useRestorableString('', 'str');
... |
Could you share your thoughts on how to face the |
I have incorporated the comments from @rrousselGit in this API proposal: Enabling restoration for hook widgetRestorationBucket? useRestoration(String? restorationId); This hook enables restoration for a hook widget. It can be used only once in a hook widget. It must have been used before other restoration hooks are used. It takes the It returns the Using a
|
What do you mean by this? |
We need a |
We don't need the It's a story similar to We could do the same thing here, and have |
If we want to avoid |
We can try an |
Looking at |
Might worth asking flutter team to do that then by just make the private method public it could be solved |
I don't think there is a good way to build on
The problem is that it's all tied to the |
I'm closing this PR since it seems to me that implementing this properly is more trouble than it's worth. |
This PR adds support for managing
RestorableProperty
s with the help of a hook andHookRestorationScope
.