-
Notifications
You must be signed in to change notification settings - Fork 166
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
Looks good! |
It's unlikely to really matter, but instead of checking that Theoretically, folks could still do: final int property;
@override
MyState createState() {
return MyState()
..property = property;
} which shouldn't be allowed either. |
Would it be better to prevent |
State subclasses definitely need instance fields. For example:
|
This is interesting. My inclination is that this might be a separate lint? (And possibly a bit more prone to false positives.) One advantage of the current advice (as you proposed it) is that it's easy to articulate and reliably enforce. Unless there are any strong objections, I'll go ahead and land this and we can consider refinements down the road. Just let me know! |
it would be interesting to run some experiments to see whether the |
I suggested Strictly speaking, it's not having constructors on In the end, there's never a situation where we'll want a custom createState/State constructor. People should use If anything, "no state constructor" is just slightly a bit less helpful than "no fancy createState". MyState global;
class MyStateful extends StatefulWidget {
@override
MyState createState() {
global = MyState();
return global;
}
} which is anti-pattern too but doesn't use constructors. Ultimately, createState has just a single possible implementation, that is: @override
MyState createState() {
return MyState();
} With maybe replacing {} + return with () =>. But i don't have any strong opinion on that one. A "no State constructor" may be easier to implement and cover most scenarios. |
I'm inclined to keep these ideas as separate rules. I'd like to land this and then consider the "no fancy createState" one. |
I'm not sure it's useful to have both. But it's your call |
Closing in favor of #1877. |
Example and docs could use some improvement (cribbed largely from the issue).
@rrousselGit @jonahwilliams ideas for improvement?
/cc @bwilkerson
Fixes dart-lang/sdk#58075