Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A sealed class in Dart is an implicitly abstract class that restricts its class hierarchy to a single library, enabling the compiler to perform exhaustive pattern matching on its subtypes. By applying the sealed modifier, you prevent the class from being extended or implemented from any external library.
// file: result_types.dart

sealed class Result {}

class Success extends Result {
  final String data;
  Success(this.data);
}

class Failure extends Result {
  final Exception error;
  Failure(this.error);
}

Core Mechanics and Rules

  • Library Encapsulation: All direct subtypes (classes that extend or implement the sealed class) must be declared in the exact same library (typically the same file) as the sealed class itself.
  • Mixin Interactions: The sealed modifier cannot be applied to a mixin declaration (i.e., sealed mixin is an invalid construct in Dart). Additionally, a sealed class cannot be used as a mixin, as Dart does not support the sealed mixin class declaration.
  • Implicitly Abstract: A sealed class cannot be instantiated directly. You do not need to (and cannot) combine the sealed modifier with the abstract modifier.
  • Exhaustiveness Checking: Because the compiler is aware of every possible subtype within the library, it enforces exhaustiveness in switch statements and switch expressions. If a new subtype is added to the sealed class, the compiler will flag an error at every switch site that does not handle the new subtype.
// The compiler guarantees all subtypes are handled.
// No 'default' or '_' wildcard branch is required.
String evaluateResult(Result result) {
  return switch (result) {
    Success(:final data) => 'Data: $data',
    Failure(:final error) => 'Error: $error',
  };
}

Subclass Inheritance Behavior

While the sealed class itself is strictly bound to its defining library, its subclasses are not implicitly sealed. By default, a subclass of a sealed class behaves like a standard Dart class and can be extended or implemented by external libraries. To maintain strict control over the entire hierarchy, you must apply additional class modifiers (final, base, or sealed) to the subclasses:
sealed class Node {}

// Can be extended outside the library
class StandardNode extends Node {} 

// Cannot be extended or implemented outside the library
final class LeafNode extends Node {} 

// Can be extended, but cannot be implemented outside the library
base class RootNode extends Node {} 

Constructor Behavior

Sealed classes can declare constructors. Because the class is implicitly abstract, these constructors cannot be invoked to create an instance of the sealed class itself. Instead, they are used to initialize shared state via super calls from the allowed subclasses, or they are defined as factory constructors that return instances of the subclasses.
Master Dart with Deep Grasping Methodology!Learn More