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 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

base mixin class Name {
  // State and behavior
}

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)
class SuperClass {}
mixin SomeMixin {}

// ERROR: A mixin class cannot have an `extends` clause.
// base mixin class InvalidExtendedSystem extends SuperClass {}

// ERROR: A mixin class cannot have a `with` clause.
// base mixin class InvalidMixedSystem with SomeMixin {}

// VALID: Directly extends Object (implicitly) and has no constructors.
base mixin class CoreSystem {
  String version = '1.0.0';
  
  // ERROR: Cannot declare ANY constructors (generative or factory) in a mixin class.
  // CoreSystem();
  // factory CoreSystem.create() => CoreSystem();

  void initialize() => print('System initialized');
}

// ALLOWED: Implementing within the same library is permitted.
// MUST be marked base, final, or sealed to satisfy modifier propagation.
base class LocalMock implements CoreSystem {
  @override
  String version = 'Mock-1.0.0';
  @override
  void initialize() => print('Mock initialized');
}

// ALLOWED: Using as an `on` constraint within the same library.
// MUST be marked base to satisfy modifier propagation.
base mixin SystemExtension on CoreSystem {
  void extendedAction() => print('Action on $version');
}
library_b.dart (Consuming Library)
import 'library_a.dart';

// ALLOWED: Instantiation (relies entirely on the implicit default constructor)
final system = CoreSystem();

// ALLOWED: Extending (Must be marked base, final, or sealed)
final class ExtendedSystem extends CoreSystem {
  void customMethod() {}
}

// ALLOWED: Mixing in (Must be marked base, final, or sealed)
base class MixedSystem with CoreSystem {
  void customMethod() {}
}

// ERROR: Cannot implement a base class outside its defining library.
// class IllegalSystem implements CoreSystem { ... }

// ERROR: All subtypes must be base, final, or sealed.
// class InvalidExtendedSystem extends CoreSystem { ... }

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.
Master Dart with Deep Grasping Methodology!Learn More