IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

ux (Software)

Reusing state logic is either too verbose or too difficult

rrousselGitMar 2, 2020, 12:19 AM

[.](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.

Comment on this issue