Skip to main content
A base mixin class in Dart is a compound class modifier that defines a construct capable of being instantiated, extended, or mixed in, while strictly prohibiting external implementation. By applying the base modifier, it guarantees that all subtypes inherit the actual implementation of the class rather than just its interface, enforcing a closed implementation hierarchy outside its defining library.

Syntax

Core Mechanics and Restrictions

The behavior of a base mixin class is the intersection of the rules governing base, mixin, and class modifiers:
  1. Inheritance Restrictions (mixin class): A mixin class cannot have an extends clause or a with clause. It must directly extend Object. It is structurally forbidden from inheriting from other classes or mixing in other mixins.
  2. Instantiation and Constructors (class / mixin class): It can be instantiated directly, provided it is not marked abstract. However, because it acts as a mixin class, it cannot declare any constructors whatsoever. This prohibition applies to both generative constructors (including an explicit empty default constructor like Name();) and factory constructors. It must rely entirely on the implicit default constructor.
  3. Extension (base / class): It can be extended using the extends keyword from any library.
  4. Mixing (mixin): It can be mixed into other classes using the with keyword from any library. It cannot declare an on clause because it is declared as a class.
  5. Implementation Restriction (base): It cannot be implemented using the implements keyword outside of the library in which it is defined.
  6. Subtype Enforcement (base): The modifier propagation (or subtype restriction) rule applies to all subtypes. Any declaration that becomes a subtype—whether by extending (extends), mixing in (with), implementing (implements, which is only allowed in the defining library), or using it as a mixin constraint (on)—must explicitly declare a modifier that preserves the base restriction. Subtype classes must be marked base, final, or sealed, and subtype mixins must be marked base.

Structural Rules Visualization

To understand the enforcement boundaries and structural limitations, consider the following cross-library interaction. library_a.dart (Defining Library)
library_b.dart (Consuming Library)

Modifier Compatibility

  • abstract: A base mixin class can be prefixed with abstract (abstract base mixin class). This removes the ability to instantiate it directly while retaining the extension, mixing, and base implementation rules.
  • Mutually Exclusive Modifiers: It cannot be combined with interface, final, or sealed, as Dart enforces a single access-control modifier per class declaration.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More