Reusing state logic is either too verbose or too difficult
[.](https://github.com/flutter/flutter/issues/51752#issuecomment-675844037) Related to the discussion around hooks #25280 TL;DR: It is difficult to reuse `State` logic. We either end up with a complex and deeply nested `build` method or have to copy-paste the logic across multiple widgets. It is neither possible to reuse such logic through mixins nor functions. # Problem Reusing a `State` logic across multiple `StatefulWidget` is very difficult, as soon as that logic relies on multiple life-cycles. A typical example would be the logic of creating a `TextEditingController` (but also `AnimationController`, implicit animations, and many more). That logic consists of multiple steps: - defining a variable on `State`. ```dart TextEditingController controller; ``` - creating the controller (usually inside initState), with potentially a default value: ```dart @override void initState() { super.initState(); controller = TextEditingController(text: 'Hello world'); } ``` - disposed the controller when the `State` is disposed: ```dart @override void dispose() { controller.dispose(); super.dispose(); } ``` - doing whatever we want with that variable inside `build`. - (optional) expose that property on `debugFillProperties`: ```dart void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('controller', controller)); } ``` This, in itself,
Posted by rrousselGit on Mar 2, 2020, 12:19 AM
Tags
Related issues
- This is the biggest problem with contracts, there's simply no way to actually say "please always execute this check".
- I don't disagree with you on this, I worked my whole life in corporate and haven't worked as a OSS maintainer before, though I will _and ...
- the new VSCode UI, looks cute, i don't like it.
- I've been working on my own private harness for the past 8 months, and I've been collecting ideas from such repos I've stumbled upon.
- The way AI is able to interact with outside resources is pretty impressive, but the quality of code it produces to me is still questionab...
- Add PR-style review comments in Git Diff UI that feed into the active agent thread
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
ux (Software)
This is the biggest problem with contracts, there's simply no way to actually say "please always execute this check".
I don't disagree with you on this, I worked my whole life in corporate and haven't worked as a OSS maintainer before, though I will _and ...
the new VSCode UI, looks cute, i don't like it.
I've been working on my own private harness for the past 8 months, and I've been collecting ideas from such repos I've stumbled upon.
The way AI is able to interact with outside resources is pretty impressive, but the quality of code it produces to me is still questionab...
Add PR-style review comments in Git Diff UI that feed into the active agent thread
Thanks for indulging me so far, by the way, I really appreciate this discussion, it's very stimulating.
Migrate GDScript to the GDExtension mechanism and overhaul the implementation
Reusing state logic is either too verbose or too difficult
[.](https://github.com/flutter/flutter/issues/51752#issuecomment-675844037) Related to the discussion around hooks #25280 TL;DR: It is difficult to reuse `State` logic. We either end up with a complex and deeply nested `build` method or have to copy-paste the logic across multiple widgets. It is neither possible to reuse such logic through mixins nor functions. # Problem Reusing a `State` logic across multiple `StatefulWidget` is very difficult, as soon as that logic relies on multiple life-cycles. A typical example would be the logic of creating a `TextEditingController` (but also `AnimationController`, implicit animations, and many more). That logic consists of multiple steps: - defining a variable on `State`. ```dart TextEditingController controller; ``` - creating the controller (usually inside initState), with potentially a default value: ```dart @override void initState() { super.initState(); controller = TextEditingController(text: 'Hello world'); } ``` - disposed the controller when the `State` is disposed: ```dart @override void dispose() { controller.dispose(); super.dispose(); } ``` - doing whatever we want with that variable inside `build`. - (optional) expose that property on `debugFillProperties`: ```dart void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('controller', controller)); } ``` This, in itself,
Comments (0)
No comments yet. Start the discussion.