Replace type code with Union type

Flutter tips Published on

Refactoring your code helps make it easier to manage and understand. One common pattern that can make code complex is using 'type code'. This is when you have an enum or other type indicator in a class and use switch statements or if/else chains to perform different actions based on that type.

The Problem: Type Code

Imagine a Document class with a DocumentType enum (pdf, doc, markdown). Inside a method like write, you might use a switch statement to decide what to do based on document.type. This works, but it means you need to add a new case every time you add a new document type. These switch statements can spread throughout your codebase, making updates tricky.

The Solution: Union Types (Sealed Classes in Dart)

Dart 3 introduces sealed classes, which are great for representing a fixed set of types. Instead of one Document class with a type field, you can create a sealed DocumentSealed class with subclasses like PdfDocument, DocDocument, and MarkdownDocument. Each subclass extends the sealed class.

How it Works

Why This Is Better

When to Use This Pattern

Use this refactoring when a class needs to behave differently based on its state or type, and you find yourself using switch statements on a type field frequently. For example, if certain operations are only possible for some document types (like you can't 'write' to a PDF in your system).

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is 'type code'?

Type code is when you use a simple value, like an enum or integer, within a class to indicate its type or state, and then use switch statements or if/else chains elsewhere in the code to act differently based on that value.

How do Union Types (Sealed Classes) help?

Instead of using a type value inside one class, you create a base sealed class and separate subclasses for each type. The specific behavior for each type is put into its corresponding subclass, removing the need for central switch statements.

Is this only for Flutter?

No, this refactoring pattern (often called 'Replace Type Code with Subclasses' or using Discriminated Unions/Sealed Classes) is a general programming concept applicable to many languages, including Dart which is used in Flutter.

When should I use this pattern?

Consider using this pattern when you have a class with a type field that causes you to write many switch statements to handle different behaviors. It's especially useful when adding new types requires modifying many parts of your code.

Read more
You may also be interested in
Flutter tips: how to create a responsive layout  blog card image
Flutter tips: how to create a responsive layout
Published on 2025-05-03
How to open the system app settings page  blog card image
How to open the system app settings page
Published on 2025-05-02
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved