-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Lint request : avoid_returning_widgets #58303
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
Comments
If anyone is interested to try this rule, we have it implemented in additional dart linter called Dart Code Metrics. Any feedback is welcome! |
/fyi @dnfield @goderbauer -- we talked about something similar the other day... |
I'm failing to see what this adds to address the concerns raised in dart-archive/linter#2582 (comment) and dart-archive/linter#2582 (comment) IOW - there seem to be valid cases where you would return a widget from a function to make code more readable. That said, passing around I'm not strongly for or against this lint. |
Both is good. Such a lint could also have that autofix on save, it would simplify creating new widgets.
The point of this lint is to stop doing that and make new widgets. The Flutter team has officially taken a stance in this. |
Any update in this request ? dcm is not free anymore |
I see the value here but have to defer to flutter folks to get a bit more momentum. |
Every concern has been addressed years ago, too. Not only that, but there's now an official stance from the Flutter team itself even if individual members don't show up. I don't think there's any reason left to hold back. |
Can you summarize the stance from the Flutter team? I apologize, but I'm not going to scour that video for what you are referring to. |
@srawlins Flutter in |
A recommendation (without knowing any more about it) is very different from a rigorous rule. In order for a rule to be high value (which may lead it to be high priority), it needs to have a very low false positive rate. If members of the Flutter framework team also would recommend that such a rule be added to the flutter_lints package, meaning it would be of high value, and have very low false positive rates, and the recommendation is very strong, like a developer would have to be in an extreme scenario in order to ever have a function return a Widget, then it could be considered high priority. Even then, it may not be implemented for many months, as it will be weighed against many other priorities. |
It is quite a serious recommendation, even backed by Remy Rousselet 2 years ago " I think this request fits well inside an optionnal linter rule. Most linter rules are not following rigorous rule, and I think this request improve perfomances significantly |
Here's a transcript for people in a hurry.
And here's that quote from Remi
|
What Dan said in #58303 and what is said in the comments linked from there is still accurate. While in general it is a good idea to prefer widget classes over functions it is not a hard rule. In fact, there are many cases where one has no other choice, but to define a function that returns a widget (for example to be passed to Builder.builder or Builder-like widgets). There is nothing wrong with that and that is by design. I don't see us adding this lint to flutter_lints or recommending it in any other way. |
Can you find other cases than the ones noted in the proposal?
There's also code examples in the proposal that directly addresses this. BAD class BadBuilder extends StatelessWidget {
const BadBuilder({Key key}) : super(key: key);
@override
Widget build(BuildContext context) => Builder(builder: _getExample);
/// Disallowed.
Widget _getExample(BuildContext context) => const Example();
} GOOD class Good extends StatelessWidget {
const Good({Key key}) : super(key: key);
/// Allowed because of `@override`.
@override
Widget build(BuildContext context) => const Example();
} class GoodBuilder extends StatelessWidget {
const GoodBuilder({Key key}) : super(key: key);
@override
Widget build(BuildContext context) => Builder(
builder:
/// Allowed because it's anonymous.
(context) => const Example(),
);
} |
+1 I'd like to be able to add this to my |
Describe the rule you'd like to see implemented
Disallow functions from returning a
Widget
except in a few exceptions.@override
is usedThere are many discussions about Functional Widgets vs Stateless Widgets and the consensus is that classes are generally better than functions. I think it would be better to get in the habit of writing widgets instead of functions early in the learning process and a linting rule can help avoid mistakes.
Alternative names :
avoid_returning_widgets
,avoid_functional_widgets
,prefer_widget_classes
.Examples
const
constructor, which would avoid unnecessary rebuilds.BAD
GOOD
Additional context
The text was updated successfully, but these errors were encountered: