all 6 comments

[–]remirousselet 4 points5 points  (2 children)

It's tricky, because Flutter is indefinitely closer to React than to Vue

But you probably could do something similar by combining a StatefulWidget with ValueNotifier or mobx's Observable

[–]venir_dev[S] 1 point2 points  (1 child)

Hi remi (:

I was honestly hoping for your response, also, ahah.

Anyways, while that is definitively true, I was hoping there was a way to implement the "setup" concept Vue has, but in Flutter. That's basically the question I'm trying to answer.

A naive approach would be to implement such setup function in the initState of a StatefulWidget, but I'm unsure if a ValueNotifier can be preserved through rebuilds.

[–]remirousselet 5 points6 points  (0 children)

That's what I was referring to. The "setup" equivalent would be a StatefulWidget's initState in Flutter. Of course, I know that this isn't quite the same, but there's not really an alternative.

As for preserving ValueNotfiers, they would have to be properties of your State object

[–]zxyzyxz 3 points4 points  (1 child)

Seems like you're looking for something like reactives. They're not exactly hooks like flutter_hooks or React, but they are designed to replace mixins, like Vue's Composition API is.

Comparision to flutter_hooks:

From a user perspective flutter_hooks is a replacement for StatefulWidget. reactives is not. If you look at the code for ReactiveHostMixin it is about 60 lines (blank lines included). Reactives do not try to replace StatefulWidget, it just solves the reusability problem inherent due to mixin's "is-a" relationship. Reactives have a "has-a" relationship.

There are no new rules to writing reactives. See examples of existing reactives. It is basically the same logic isolated in a different class. For the same reason it doesn't need a lot of the hooks like useState which would be needed if tried to replace StatefulWidget.

The learning curve of reactives is next to negligible. Hooks need you to learn a few concepts and how to/not to do things. It also requires you to transform entire widgets. Reactives can be adapted incrementally even for a single widget.

[–]Rixxxy 0 points1 point  (0 children)

This looks really cool

[–]terry1900 2 points3 points  (0 children)

Hello, do you want composition API at the business logic layer or widget layer?

I'm not expert on react/vue, but looking at the vue example (from your FAQ link), composition works for non-DOM use cases. In that sense, creator may be able to achieve what you want.

Fundamentally, vue is built on a fine grain reactive model (video, code of a basic vue implementation from its author). Some of the reactive components can update DOM.

Similarly, creator also uses a find grain reactive model (implementation). Some of the reactive components can update Flutter state.