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 class in Dart is a blueprint for creating objects that encapsulates state through instance variables and behavior through methods. Dart is a purely object-oriented language, meaning every object is an instance of a class, and all classes ultimately descend from the Object class (with the exception of Null). Dart supports single inheritance, implicit interfaces, and mixin-based multiple inheritance.

Anatomy of a Dart Class

A standard class definition includes instance variables (fields), constructors, and methods. Dart uses an underscore (_) prefix to denote library-private visibility for fields, methods, or the class itself.
class Entity {
  // Public instance variable
  String id;
  
  // Library-private instance variable
  int _internalState;

  // Generative constructor using initializing formal parameters
  Entity(this.id, this._internalState);

  // Instance method
  void updateState(int newState) {
    _internalState = newState;
  }
}

Constructors

Dart provides multiple constructor types to handle different instantiation mechanics and memory management strategies.
  • Generative Constructors: The standard mechanism for allocating a new instance.
  • Named Constructors: Allows a class to define multiple constructors with distinct names.
  • Factory Constructors: Uses the factory keyword. It does not strictly allocate a new instance; it can return an existing instance from a cache or an instance of a subtype.
  • Constant Constructors: Uses the const keyword to create compile-time constants. All instance variables must be final.
class Vector {
  final double x;
  final double y;

  // Generative constructor
  Vector(this.x, this.y);

  // Named constructor with an initializer list
  Vector.origin() : x = 0.0, y = 0.0;

  // Constant constructor
  const Vector.immutable(this.x, this.y);

  // Factory constructor
  factory Vector.fromList(List<double> coordinates) {
    if (coordinates.length < 2) throw ArgumentError();
    return Vector(coordinates[0], coordinates[1]);
  }
}

Properties (Getters and Setters)

Dart abstracts field access. Instance variables implicitly generate getters (and setters for non-final variables). You can override this behavior using the get and set keywords to compute properties dynamically.
class Box {
  double width;
  double height;

  Box(this.width, this.height);

  // Getter
  double get area => width * height;

  // Setter
  set area(double value) {
    width = value / height;
  }
}

Inheritance, Interfaces, and Mixins

Dart handles class hierarchies and composition through three distinct keywords: extends, implements, and with.
  • extends: Creates a subclass via single inheritance. The subclass inherits both the interface and the implementation of the superclass.
  • implements: Treats a class as an interface. Every class in Dart implicitly defines an interface containing all its instance members. Using implements requires the subclass to provide implementations for all members of the target class, inheriting no behavior.
  • with: Applies a mixin, allowing the reuse of a class’s code in multiple class hierarchies without requiring a shared superclass.
class Base {}
class Contract {}
mixin Observable {}

class Concrete extends Base with Observable implements Contract {
  // Inherits implementation from Base and Observable
  // Must provide implementation for all members of Contract
}

Class Modifiers (Dart 3+)

Dart 3 introduced class modifiers that strictly control how a class can be instantiated, extended, or implemented from outside its defining library.
  • abstract: The class cannot be instantiated. It can contain abstract methods (methods without a body).
  • base: The class can be instantiated and extended (extends), but cannot be implemented (implements). Guarantees the base class implementation is always executed.
  • interface: The class can be instantiated and implemented, but cannot be extended.
  • final: The class can be instantiated, but cannot be extended or implemented outside its library.
  • sealed: The class cannot be instantiated. It creates a closed, exhaustively switchable type hierarchy. All direct subtypes must be defined in the same library.
// Cannot be instantiated; acts as a strict contract
abstract interface class Repository {
  void save();
}

// Exhaustive hierarchy root
sealed class Result {}
class Success extends Result {}
class Failure extends Result {}
Master Dart with Deep Grasping Methodology!Learn More