ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.
Core Mechanics and Rules
- Library Encapsulation: All direct subtypes (classes that
extendorimplementthe sealed class) must be declared in the exact same library (typically the same file) as the sealed class itself. - Mixin Interactions: The
sealedmodifier cannot be applied to amixindeclaration (i.e.,sealed mixinis an invalid construct in Dart). Additionally, asealedclass cannot be used as a mixin, as Dart does not support thesealed mixin classdeclaration. - Implicitly Abstract: A sealed class cannot be instantiated directly. You do not need to (and cannot) combine the
sealedmodifier with theabstractmodifier. - Exhaustiveness Checking: Because the compiler is aware of every possible subtype within the library, it enforces exhaustiveness in
switchstatements andswitchexpressions. If a new subtype is added to the sealed class, the compiler will flag an error at everyswitchsite that does not handle the new subtype.
Subclass Inheritance Behavior
While thesealed 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:
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 viasuper 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





