Skip to content
Blog

Constant Primary Constructors in Dart 3.13: Immutability & Performance

Declaring const primary constructors in class headers, canonicalized immutable objects in Flutter, and compile-time constants.

Published on • October 24, 2026

AI Assistant

Immutable data structures are essential for building predictable, jank-free Flutter applications. In Dart 3.13, constant primary constructors allow developers to define immutable compile-time constant classes directly within class header signatures.

In this deep dive, we explore how const primary constructors work, how the Dart VM canonicalizes constant instances, and best practices for Flutter state management.

How Constant Primary Constructors Function

When a class declares a const primary constructor in its header, all instance fields declared in the body or signature must be marked final:

// Constant primary constructor in Dart 3.13
class ThemeColorConfig const (
  this.primaryColor,
  this.accentColor, {
  this.isDarkMode = false,
}) {
  final int primaryColor;
  final int accentColor;
  final bool isDarkMode;
}

By adding const before the parameter list in the class header:

  1. Instances created with const ThemeColorConfig(0xFF1E1E2E, 0xFF89B4FA) are evaluated at compile time.
  2. The Dart VM canonicalizes identical constant instances into a single memory location, reducing RAM overhead.
  3. Flutter’s widget tree skips re-indexing constant sub-trees during rebuild cycles.

Integrating Constant Primary Constructors in Flutter Widgets

In Flutter, widgets with const constructors drastically improve frame rates during complex animations by preventing unnecessary widget allocations:

class MetricTile const (
  this.title,
  this.value, {
  super.key,
}) extends StatelessWidget {
  final String title;
  final String value;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
          Text(value),
        ],
      ),
    );
  }
}

Factory Redirects and Constant Value Caching

Combine constant primary constructors with factory constructors to implement zero-allocation caching patterns:

class AppDimensions const (this.width, this.height) {
  final double width;
  final double height;

  // Pre-cached canonical constants
  static const AppDimensions zero = AppDimensions(0.0, 0.0);
  static const AppDimensions standardTile = AppDimensions(100.0, 100.0);
}

Summary

Constant primary constructors combine immutability, performance optimizations, and concise syntax in Dart 3.13.

References & Further Reading