Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Use Annotation connect to localstate #66

Closed
lyquocnam opened this issue Jul 30, 2018 · 3 comments
Closed

[Feature Request] Use Annotation connect to localstate #66

lyquocnam opened this issue Jul 30, 2018 · 3 comments

Comments

@lyquocnam
Copy link

lyquocnam commented Jul 30, 2018

Dear author, i think this feature will make more perfect.
example:

class MyWidget extends StatelessWidget {
  
  @store('hello')
  String hello;

  @override
  Widget build(BuildContext context) {
    return Text(hello);
  }

}

this will get automatically get hello state from store and auto dispatch when hello variable are set.
i think this is hard part, but may be it possible.

ps: sorry about my English 👯‍♂️

@brianegan
Copy link
Owner

brianegan commented Jul 30, 2018

Hey there -- thanks for writing in!

Hrm, I'm honestly not sure I could properly support this. For me, two questions come to mind: Where does "hello" come from, and what action would be dispatched when the value is set so that you could handle the change in your reducer?

If you're more interested in Getter / Setter type of Architectures, you might want to consider something like scoped_model instead of Redux. It will allow you to create something like what you want, e.g.:

class MyModel extends Model {
  String _hello = 'Hi there';

  String get hello => _hello;

  void set hello(String newValue) {
    hello = newValue;
    notifyListeners();
  }
}

Then in your Widgets, you could access the value like so, and this Widget be automatically rebuilt whenever the Model changes:

ScopedModelDescendant<CounterModel>(
  builder: (context, child, model) {
    return FlatButton(
      child: Text(model.hello),
      onPressed: () {
        model.hello = 'Yo Yo'
      }
    );
  }
);

@lyquocnam
Copy link
Author

lyquocnam commented Jul 30, 2018

ya you are right.

class MyWidget extends StatelessWidget {
  
  @store('hello', HelloAction)  // <----
  String hello;

  @override
  Widget build(BuildContext context) {
    return Text(hello);
  }
}

if does not have action, it's just get from store, and with action, it will dispatch HelloAction and reducer work in behind the scenes.
Sometime i need to use some states outside Builder, so this way i can do it easily

@brianegan
Copy link
Owner

Hey there -- overall, I can see why you'd like to use this, but I don't think there'd be an easy way to support this.

For example, where does the 'hello' value come from? Is it from state.hello? What if you need data that's nested deeper than that, such as state.todos[1] or state.user.isLoggedIn, for example? How would it remain type-safe (what if 'hello' wasn't a String but an int, would you get a type error?)

While I can see the usefulness of this feature, I think it might be better to try experimenting with this in a separate library. The only way I could see making this work is through code generation, and I'd like to keep that out of this lib for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants