rrousselGit / flutter_hooks

React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
MIT License
3.13k stars 179 forks source link

Need clarification on Flutter_hooks docs #340

Closed skillkrio closed 1 year ago

skillkrio commented 1 year ago

Dart mixins can partially solve this issue, but they suffer from other problems:

  1. A given mixin can only be used once per class.

I didn't understand the above one. Can you explain it further an example would be appreciated @rrousselGit

rrousselGit commented 1 year ago

Say you made a mixin for creeating + disposing a TextController:

mixin  MyMixin on State<StatefulWidget> {
  final controller = TextController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

That works fine for creating one controller. But if you want to create two of them, you can't apply the mixin twice to create two independent controller:

class MyState extends State<MyWidget> with MyMixin, MyMixin {

}

Whereas with hooks, the equivalent would be using useTextEditingController twice:

Widget build(context) {
  final controller1 = useTextEditingController();
  final controller2 = useTextEditingController();

}
skillkrio commented 1 year ago

Wonderful Example. Thanks alot @rrousselGit .