dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
636 stars 170 forks source link

proposal: `ternary_operator_with_empty_widget` #3875

Open asashour opened 1 year ago

asashour commented 1 year ago

ternary_operator_with_empty_widget

Description

Avoid having a ternary condition operator with an empty widget inside a collection.

Details

There is no need to have a ternary operator when one of the expressions is an empty widget, e.g. SizedBox() or Container(). A collection if can be used instead.

Kind

Avoid

Bad Examples

Column(
  children: [
    inTheSky ? const Text('Flying') : const SizedBox(),
  ],
),

Column(
  children: [
    inTheSky ? const Text('Flying') : Container(),
  ],
),

Column(
  children: [
    !inTheSky ? const SizedBox.shrink() : const Text('Flying'),
  ],
),

Good Examples

Column(
  children: [
    if (inTheSky) const Text('Flying'),
  ],
),

Discussion

Having if inside an array, is probably not intuitive for new users.

Discussion checklist

bwilkerson commented 1 year ago

How often do users produce code that looks like this unintentionally?

That is, how big of a problem is it?

How often do users produce code that looks like this intentionally in order to keep the right layout? How often to users temporarily produce code that looks like this, intending to populate the children of the widget?

That is, how often will this result in false positives?

My guess is that it will produce false positives more often than it will catch valid errors.

@goderbauer For a more informed opinion.