Skip to content

Create a ValueListenableBuilder - #19729

Merged
xster merged 5 commits into
flutter:masterfrom
xster:value_listenable_builder
Aug 16, 2018
Merged

xster merged 5 commits into
flutter:masterfrom
xster:value_listenable_builder

Conversation

@xster

@xster xster commented Jul 24, 2018

Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I test that the listeners are indeed removed when I remove the builder from the tree

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would just subclass ChangeNotifier in your test. But this is fine too I guess.

@Hixie

Hixie commented Jul 30, 2018

Copy link
Copy Markdown
Contributor

How does this differ from an AnimationBuilder?

@xster

xster commented Jul 30, 2018 •

Copy link
Copy Markdown
Member Author

It gives you the value of the listenable so the builder function can build from it without the owner of this widget needing to capture the source of the data again to re-plumb it into the build function which seems like a leak of abstraction.

The AnimatedWidget API seems to be made to be suitable for subclassing (where the subclass owns the listenable anyway) rather than use by composition (like the FutureBuilder, StreamBuilder API).

@Hixie

Hixie commented Aug 1, 2018

Copy link
Copy Markdown
Contributor

I meant AnimationBuilder, not AnimatedWidget. As far as I can tell this is identical except for passing the value through, but it's not clear how useful that is. You always have the ValueListenable when building this widget, obviously, so using it in the closure passed to builder seems straight-forward?

@xster

xster commented Aug 1, 2018

Copy link
Copy Markdown
Member Author

I guess I was referring to the AnimatedBuilder too (just that the meat of it is inside AnimatedWidget).

Half of it is the discoverability. We can't do much about it now but 'Animated'Builder would definitely throw people off.

The other half is still to avoid the leak of abstraction. The owner of the ValueListenableBuilder should be just compose things (the listenable and the handler) without wanting to know how to open either of them.

Imagine an owner that has its own problem domain and then also knows how to retrieve a listenable via some injection mechanism and is given a function instance. It should be able to just pass the function to the ValueListenableBuilder it creates without first unpacking the ValueListenable itself and then re-invoke the function in a specific way.

@xster
xster force-pushed the value_listenable_builder branch 2 times, most recently from ebdfe80 to 0773c74 Compare August 8, 2018 04:50
@xster
xster force-pushed the value_listenable_builder branch 2 times, most recently from 2824f6e to 26a5ef3 Compare August 10, 2018 17:29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds a listener, and thus leaks listeners.

To avoid this kind of accidental leak, I recommend always being symmetric when adding and removing listeners.
So you would explicitly add one in initState, remove one in dispose, and add/remove them in didUpdateWidget.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to add a test for this too :-)

@Hixie

Hixie commented Aug 15, 2018

Copy link
Copy Markdown
Contributor

LGTM

@xster
xster force-pushed the value_listenable_builder branch from 26a5ef3 to 2c8cc04 Compare August 15, 2018 21:13
@xster
xster merged commit ea355c6 into flutter:master Aug 16, 2018
@xster
xster deleted the value_listenable_builder branch August 16, 2018 02:22
goderbauer added a commit that referenced this pull request Aug 16, 2018
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants