data_classes 2.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

Hey there! If you're reading this and want data classes to become a language-level feature of Dart, consider giving this issue a thumbs up. 👍

In the meantime, this library generates immutable data classes for you based on simple mutable blueprint classes. Here's how to get started:

1. 📦 Add these packages to your dependencies:

  data_classes: [version]

  build_runner: [version]
  data_classes_generator: [version]

2. 🧬 Write a blueprint class. Let the name start with Mutable and annotate it with @GenerateDataClassFor():

import 'package:data_classes/data_classes.dart';

part 'my_file.g.dart';

class MutableFruit {
  String type;
  @nullable String color;

By default, attributes are considered non-nullable. If you want an attribute to be nullable, annotate it with @nullable.

3. 🏭 Run pub run build_runner build in the command line (or flutter pub run build_runner build, if you're using Flutter). The implementation based on your mutable class will automatically get generated.

The immutable class contains

  • a constructor with named parameters and assertions for values that shouldn't be null,
  • method/constructor for converting the immutable class to the mutable class and the other way around,
  • custom implementations of == and hashCode as well as toString(),
  • a copy method.

For example, here's the generated code of our 5-line-class above:

/// This class is the immutable pendant of the [MutableFruit] class.
class Fruit {
  final String type;
  final String color;

  /// Default constructor that creates a new [Fruit] with the given attributes.
  const Fruit({
    @required this.type,
  }) : assert(type != null);

  /// Creates a [Fruit] from a [MutableFruit].
  Fruit.fromMutable(MutableFruit mutable)
      : type = mutable.type,
        color = mutable.color;

  /// Turns this [Fruit] into a [MutableFruit].
  MutableFruit toMutable() {
    return MutableFruit()
      ..type = type
      ..color = color;

  /// Checks if this [Fruit] is equal to the other one.
  bool operator ==(Object other) {
    return other is Fruit && type == other.type && color == other.color;

  int get hashCode => hashList([

  /// Copies this [Fruit] with some changed attributes.
  Fruit copy(void Function(MutableFruit mutable) changeAttributes) {
        changeAttributes != null,
        "You called Fruit.copy, but didn't provide a function for changing "
        "the attributes.\n"
        "If you just want an unchanged copy: You don't need one, just use "
        "the original.");
    var mutable = this.toMutable();
    return Fruit.fromMutable(mutable);

  /// Converts this [Fruit] into a [String].
  String toString() {
    return 'Fruit(\n'
        '  type: $type\n'
        '  color: $color\n'

[2.0.2] - 2019-10-09

  • Make freshApple in example const.

[2.0.1] - 2019-09-20

  • Revise readme: Little typo fixes and document build_runner dependency.

[2.0.0] - 2019-09-20

  • Change @DataClass() annotation to @GenerateDataClassFor().
  • GeneratedClass.fromMutable() is now a normal constructor instead of a factory constructor.
  • Provide new example.
  • Revise readme.
  • New license.

[1.1.0] - 2019-09-05

  • Change @Nullable() annotation to @nullable.

[1.0.2] - 2019-09-04

  • Rename example's mutable class to MutableUser.

[1.0.1] - 2019-09-04

  • Added example.

[1.0.0] - 2019-09-04

  • Initial release: Supporting DataClass and Nullable annotations.


import 'package:data_classes/data_classes.dart';

part 'main.g.dart';

void main() {
  const freshApple = const Fruit(type: 'apple', color: 'green');
  var someApple = freshApple.copy((fruit) => fruit..color = null);
  var kiwi = someApple.copy((fruit) => fruit
    ..type = 'Kiwi'
    ..color = 'brown');

class MutableFruit {
  String type;

  String color;

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  data_classes: ^2.0.3

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:data_classes/data_classes.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:data_classes/data_classes.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
meta ^1.1.7 1.1.7