Replace sealed class type cast to type checking

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

Flutter tips · Published on

Simplify Your Code with Smart Type Checking

Flutter development keeps getting better, and so can your code! If you've been working with sealed classes and found yourself writing separate lines to check a type and then cast it, there's a much cleaner way now.

The Old Way: Separate Type Checking and Casting

Before, you might have written code that first checked if an object was a certain type using is, and then, in a separate step, explicitly cast it using as. This method works, but it can make your code longer and a bit less direct.

Example of Old Type Casting

Imagine you had an object and needed to ensure it was a TaskRecurrentData to access its dailyStatuses. You'd check if (this is TaskRecurrentData) and then cast (this as TaskRecurrentData) to get to the dailyStatuses property.

The New Way: Direct Type Checking and Destructuring

Good news! With recent updates, you can now combine these steps. When you're using sealed classes, you can leverage pattern matching to check the type and immediately pull out the specific data you need—all at once. This process is often called "destructuring," and it helps you extract parts of an object directly into new variables.

Why This Is Better

This new approach offers several key benefits:

  • Cleaner Code: You write less code, which makes it easier to read and understand what's happening.
  • Safer: It helps prevent common errors by ensuring you're working with the correct type right from the start.
  • Simultaneous Validation: You can even add extra conditions (like when dailyStatuses != null && dailyStatuses.isNotEmpty) directly within your type check. This allows you to validate your data as you extract it.

Essentially, you no longer need to explicitly "cast" your type in a separate line. The Dart language smartly handles it when you match the pattern. This makes managing different states within a sealed class much more elegant and efficient, leading to much neater and more robust Flutter apps!

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

What is a sealed class in Flutter/Dart?

A sealed class in Dart is a class that can only be extended or implemented within the same library. This means you know all its possible subclasses, which is great for handling different states or types in a structured and exhaustive way.

Why should I replace type casting with type checking?

Replacing explicit type casting (like `as`) with modern type checking methods (like pattern matching with `case`) makes your code safer, cleaner, and easier to read. It allows you to check the type and access its members in one go, reducing unnecessary boilerplate.

Does this new pattern matching approach work for all classes?

While pattern matching and destructuring are broadly applicable in Dart, they are particularly powerful and elegant when used with sealed classes. This is because sealed classes guarantee a fixed set of known subtypes, allowing for exhaustive checks and cleaner state management.

What are the benefits of destructuring with type checking?

Destructuring allows you to extract properties from an object directly into new variables as part of the type checking process. This simplifies your code by removing the need for intermediate variables or explicit casting, making it especially useful for complex objects or different state representations.

Will adopting this change affect my older Flutter projects?

This feature is part of newer Dart language versions. While existing code using `is` and `as` will continue to function, adopting these new pattern matching and destructuring techniques can significantly improve the readability and maintainability of your codebase in newer projects or when refactoring existing ones.

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