pub package pub package

The purpose of this library is to expose the generation of very simple class methods with excellent performance and little code generation. For this you will only be able to enable what you need of the supported features


Auto generation of:

  • x Inheritance and generic classes supported
  • x hashCode and == methods
  • x pretty toString method
  • x copyWith method
  • x *Changes class and change, toChanges methods in data class
  • x *Builder class to build your class

Install package

To use DataClass, you will need your typical build_runner/code-generator setup. First, install build_runner, data_class, data_class_generator by adding them to your pubspec.yaml file:

# pubspec.yaml


Run the generator

To run the code generator you can use:

  • <dart|flutter> pub run build_runner build

As such, a file that wants to use DataClass will start with:

import 'package:mek_data_class/mek_data_class.dart';

part 'my_file.g.dart';


You can see some examples in


Because the boiler plate is generated as a mixin, it is minimally intrusive on the interface of the class. You only have to provide a constructor with named arguments for all fields and extend the generated mixin.

class Product with _$Product {
  final String title;
  @DataField(equality: DefaultEquality())
  final double price;

  const Product({
    required this.title,
    required this.price,
  String get titlePrice => '$title$price';

Customization of the equal operator and hashcode through the use of the Equality class is supported. See example.


Taking into consideration the previous example you can write and inherit all methods

class PrettyProduct extends Product with _$PrettyProduct {
  final String color;

  const Product({
    required String title,
    required double price,
    required this.color,
  }) : super(title: title, price: price);

  String get titlePriceColor => '$titlePrice$color';


You can also declare classes with generic types

class Value<T> with _$Value<T> {
  final T value;

  const Product({
    required this.value,

Pretty string

Use the ClassToString package to perform the toString method

final product = Product(...);
/// Product(
///   title=Overlord,
///   price=12,
/// )


The classic copyWith, need explanations? No, but try to prefer using *Changes which supports nullability

final product = Product(...);
print(product.copyWith(title: 'Raisekamika'));

Enable in build.yaml with copyable: true


Unlike a builder you cannot set values to null but the field is not defined as such and cannot be instantiated

final updatedProduct = product.change((changes) => changes..title = 'Raisekamika');

final changes = product.toChanges();
changes.title = 'Raisekamika';
final updatedProduct =;

Enable in build.yaml with changeable: true


Update the *Changes class by passing a function

final updatedChanges = changes.update((c) => c..title = 'Albedo'); // title=Albedo


Update the properties of the *Changes class with the properties of the DataClass

final updatedChanges = productChanges.replace(product); // title=Raisekamika


Build the DataClass from *Changes class

Product product =;


Build your class using a builder. It is not safe to construct a class using a builder but it allows you to complete the construction whenever you want.

@DataClass(buildable: true)
class Product with _$Product {
  final int id;
  const Product({required});
  factory Function(ProductBuilder b) updates) => 

final builder = ProductBuilder(); = 12;
final product =;


Generate a class that contains the names of the fields of the Data Class. @DataClass(createFieldsClass) Example:

@DataClass(createFieldsClass: true)
class Product with _$Product {
  final int id;
  const Product({required});
class ProductFields {
  final String _path;
  const ProductFields([this._path = '']);
  String get id => '${_path}id';

Global Configs

See the docs of the DataClass class for more information

Key Default Description
page_width 80 adjust the page formatting width of the generated dart code
stringify_if_null true if set to false, null values will not be included in the toString
stringify_type params if set to fields, fields of a class that are not passed to the constructor will also be included
fields_class_visible true if set to false, the fields classes is private
# build.yaml
        enabled: true
          page_width: 80
          comparable: true
          stringify: true
          stringify_type: params | fields
          stringify_if_null: true
          buildable: false
          copyable: false
          changeable: false
          changes_visible: false
          create_fields_class: false
          fields_class_visible: true
comparable: true
stringify: true
changeable: true


  • Some packages generate a lot of code and mess with the normal, classic class construction in dart. Also you can't easily select classes without any problem, without having to create mixins for the methods
  • Some packages require you to mark all your fields with @override

Similar packages freezed, built_value, dataclass_beta, functional_data