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

Use CodeGen to reduce boilerplate #2

Open
MisterJimson opened this issue Sep 5, 2019 · 3 comments
Open

Use CodeGen to reduce boilerplate #2

MisterJimson opened this issue Sep 5, 2019 · 3 comments

Comments

@MisterJimson
Copy link
Contributor

Quite a few lines per property are needed currently, and you need to keep Strings of your property names around, which is error prone and not compile time safe.

The below example takes 10 lines to implement 1 property.

class MyModel with PropertyChangeNotifier<String> {
  int _foo = 0;
  int get foo => _foo;

  set foo(int value) {
    _foo = value;
    notifyListeners('foo');
  }

  set bar(int value) {
    _bar = value;
    notifyListeners('bar');
  }
}

Recommended syntax

class MyModel with PropertyChangeNotifier<String> extends $MyModel {

 @PropertyNotifiy
  int _$foo = 0;
}

The idea is that the required code would be generated, creating a public getter and setter of foo in $MyModel. Then, extending $MyModel gives you those properties on MyModel. This reduces the lines of code to 2 per property and removes the property name String.

@MisterJimson
Copy link
Contributor Author

Actually, the generated $MyModel can extend the PropertyChangeNotifier as an option as well, so you don't need to have both in the class definition.

@martinrybak
Copy link
Owner

That would be a nice addition! I'm not that familiar with codegen, would you mind making a PR?

@MisterJimson
Copy link
Contributor Author

I may be able to, I recently started a project very similar to this, but codegen was a mandatory feature for me to reduce lines of code to implement each property. May just move my efforts here instead.

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