Mobile Development 14 min read

Implementing Data Classes and Sealed Classes in Dart (Comparison with Kotlin)

This article explains how to replicate Kotlin's data class and sealed class features in Dart for Flutter development, covering manual implementations, popular packages such as freezed, equatable, and built_value, as well as the new sealed class support introduced in Dart 3.0.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Implementing Data Classes and Sealed Classes in Dart (Comparison with Kotlin)

Dart Data Class and Sealed Class Implementation

In modern programming, Kotlin provides data class and sealed class features that simplify model creation and enforce closed hierarchies. This article explores how similar functionality can be achieved in Dart for Flutter development.

Kotlin data class and sealed class overview

Kotlin data classes automatically generate equals() , hashCode() , toString() , copy() and component functions, reducing boilerplate for simple models or DTOs. Kotlin sealed classes define a restricted inheritance hierarchy where all subclasses must be declared in the same file, enabling exhaustive when expressions.

data class User(val name: String, val age: Int)
sealed class Result
    data class Success(val data: String) : Result()
    data class Error(val error: String) : Result()

How to achieve data‑class functionality in Dart

Dart does not have built‑in data classes, so developers use several approaches:

Manually implement == , hashCode , toString and a copyWith method.

Use built_value to generate immutable value types.

Use freezed , a code‑generation package that creates immutable classes with == , hashCode , toString , copyWith and pattern‑matching support.

Use equatable to simplify equality comparison by defining a props list.

Example of a freezed class:

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'my_class.freezed.dart';

@freezed
class MyClass with _$MyClass {
  const factory MyClass({String? name, int? age}) = _MyClass;
}

Example of an equatable class:

import 'package:equatable/equatable.dart';

class MyClass extends Equatable {
  final String name;
  final int age;

  MyClass(this.name, this.age);

  @override
  List
get props => [name, age];
}

How to simulate sealed classes in Dart

Before Dart 3, developers emulate sealed classes by defining an abstract base class and a closed set of subclasses:

abstract class HomePageEvent {
  const HomePageEvent();
}

class HomePageDots extends HomePageEvent {
  final int index;
  const HomePageDots(this.index);
}

The freezed package can also generate sealed‑class‑like unions:

@freezed
abstract class HomePageEvent with _$HomePageEvent {
  const factory HomePageEvent.dots(int index) = HomePageDots;
  const factory HomePageEvent.anotherEvent(String value) = AnotherHomePageEvent;
}

With Dart 3.0, the language now supports native sealed class declarations, providing the same compile‑time exhaustiveness checks as Kotlin:

sealed class ArticleState {
  final List
data;
  const ArticleState({this.data = const []});
}

class ArticleLoading extends ArticleState {
  const ArticleLoading({List
data = const []}) : super(data: data);
}

class ArticleWithData extends ArticleState {
  final int total;
  const ArticleWithData({List
data = const [], this.total = 0}) : super(data: data);
}

class ArticleFailed extends ArticleState {
  final String error;
  const ArticleFailed(this.error, {List
data = const []}) : super(data: data);
}

Developers can choose the approach that matches their project’s Dart SDK version and compatibility requirements (e.g., avoiding Dart 3 for HarmonyOS compatibility).

Conclusion

For Flutter projects, freezed offers the most automated solution for both data classes and sealed‑class patterns, while equatable provides a lightweight way to handle equality without code generation. Manual implementations or IDE plugins like “Dart Data Class” can also fill gaps when developers prefer direct control.

DartFlutterKotlinEquatableData ClassFreezedSealed Class
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.