Advanced Merge Case Logic

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

Flutter tips · Published on

Simplify Your Switch Statements

Writing clean and efficient code is crucial for any Flutter developer. Sometimes, you might find yourself writing very similar switch cases for different object types that share common properties. Good news! Dart offers a neat trick to make your code much tidier.

The Power of Logical-Or (||) in Switch Cases

You can use the logical-or pattern (||) to group different object types together in a single switch case. This works great when these object types share the same property names and types, allowing you to handle them uniformly.

Before: Repetitive Switch Cases

Imagine you have a function that needs to count daily statuses from different data types. Without the merge case logic, your code might look a bit repetitive:

int dailyStatusesCount() {
  return switch (this) {
    TaskRecurrentData(:final dailyStatuses) when dailyStatuses != null => dailyStatuses.length,
    HabitTaskWithCounter(:final dailyStatuses) when dailyStatuses != null => dailyStatuses.length,
    _ => 0,
  };
}

Notice how TaskRecurrentData and HabitTaskWithCounter have very similar logic? We can improve this!

After: Cleaner Code with Merged Cases

Here's how you can simplify it using the logical-or pattern:

int dailyStatusesCount() {
  return switch (this) {
    TaskRecurrentData(:final dailyStatuses) || HabitTaskWithCounter(:final dailyStatuses)
    when dailyStatuses != null => dailyStatuses.length,
    _ => 0,
  };
}

This makes your code shorter, easier to read, and less prone to errors because you're maintaining logic in one place.

Unified Variable Extraction

A cool feature that comes with this technique is "Unified Variable Extraction." When you use :final dailyStatuses in both patterns, Dart is smart enough to extract the value into a single local variable. This dailyStatuses variable then becomes available for use on the right-hand side of the => in your switch case. This means you don't need to declare dailyStatuses multiple times; Dart handles it for you efficiently.

By adopting advanced merge case logic, you can significantly streamline your Flutter code, making it more maintainable and elegant.

Save 3 months of work

One command. Pick your modules. Firebase or Supabase auto-configured. Start building what matters.

kickstarter for flutter apps

Frequently Asked Questions

Q: What is 'Advanced merge case logic' in Flutter/Dart?

A: It's a technique that lets you combine multiple `switch` case patterns into a single case using the logical-or (`||`) operator. This works for different object types that share similar properties, making your code more concise and readable.

Q: How does the logical-or pattern (`||`) work in a `switch` statement?

A: The `||` pattern allows you to specify multiple types or patterns that should be handled by the same block of code. If the `switch` expression matches any of the patterns listed, that case will execute, simplifying your conditional logic.

Q: What is 'Unified Variable Extraction'?

A: This is when Dart automatically extracts a common variable (like `:final dailyStatuses` in the example) from multiple patterns grouped by `||`. This extracted variable can then be used in the logic following the `=>`, reducing redundancy and improving code clarity.

Q: When should I use this advanced merge case logic?

A: You should use it when you have different data types or objects that need to be processed in a very similar way within a `switch` statement, especially if they share common property names and types. It helps reduce code duplication and improves readability.

Q: Are there any limitations to using this technique?

A: The main requirement is that the patterns being merged must have compatible variable extractions. If you try to extract variables with the same name but different types, or if the patterns are too dissimilar, Dart will flag an error. It's best suited for cases where the patterns are structurally similar.

Read more
You may also be interested in
Made by ApparenceKit logo
ApparenceKit is a flutter start kit | template generator tool by Apparence.io © 2026.
All rights reserved