prefer_switch_expression
v0.3.0WarningFixConfigurableControl Flow
many_lints.yaml
many_lints.yaml
analysis_options.yaml many_lints.yaml
Warns when a switch statement can be converted to a switch expression. This applies when all branches either return a value or assign to the same variable, with each case containing exactly one statement.
Why use this rule
Section titled “Why use this rule”Dart 3 introduced switch expressions as a more concise alternative to switch statements for simple value-producing switches. They reduce boilerplate (case, return, break), make it clear that the switch produces a value, and are easier to read when each branch is a single expression. The quick fix handles the conversion automatically.
See also: Switch expressions
// All cases return a value — use switch expressionDeliveryIcon iconForBad(DeliveryStage stage) { switch (stage) { case DeliveryStage.packed: return DeliveryIcon.box; case DeliveryStage.shipped: return DeliveryIcon.truck; case DeliveryStage.delivered: return DeliveryIcon.home; }}
// All cases assign to the same variableString getDescriptionBad(DeliveryIcon icon) { String description; switch (icon) { case DeliveryIcon.box: description = 'Waiting in the warehouse'; case DeliveryIcon.truck: description = 'On the road'; case DeliveryIcon.home: description = 'Dropped at the door'; } return description;}// Switch expression with returnDeliveryIcon iconForGood(DeliveryStage stage) { return switch (stage) { DeliveryStage.packed => DeliveryIcon.box, DeliveryStage.shipped => DeliveryIcon.truck, DeliveryStage.delivered => DeliveryIcon.home, };}
// Switch expression with assignmentString getDescriptionGood(DeliveryIcon icon) { final description = switch (icon) { DeliveryIcon.box => 'Waiting in the warehouse', DeliveryIcon.truck => 'On the road', DeliveryIcon.home => 'Dropped at the door', }; return description;}
// Switch expression with default case (using wildcard)String getNameGood(int value) { return switch (value) { 1 => 'one', 2 => 'two', _ => 'unknown', };}Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_switch_expression: trueTo turn it off again:
rules: prefer_switch_expression: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”many_lints: rules: prefer_switch_expression: allow_fallthrough_cases: truerules: prefer_switch_expression: allow_fallthrough_cases: true| Option | Type | Default | Description |
|---|---|---|---|
allow_fallthrough_cases |
bool | false |
Also report switches where labels share a body. The quick fix merges them into a single `case a |
Related rules
Section titled “Related rules”avoid_nested_conditional_expressions— Flag a conditional nested inside another.prefer_conditional_expressions— Collapse a two-way if/else into a conditional expression.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.no_equal_switch_case— Flag two switch branches with identical bodies.