dataclass 0.3.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

dataclass #

License Github Stars

@DataClass annotation used for dataclass_generator

The DataClass generator generates base class for your data class with methods:

  • equals (operator ==)
  • hashCode
  • toString
  • copyWith

Getting Started #

0. Add dependencies to pubspec.yaml #

  dataclass: latest_version
  dataclass_generator: latest_version

1. Annotate your class with @dataClass #

The class should:

  • has only final fields
  • unnamed constructor with named parameters for all fields
class Car {
  final String name;
  final String manufacturer;
  final double price;

  Car({, this.manufacturer, this.price});

2. Generate dataclass base class #

Run pub run build_runner build

Generated file

abstract class _$Car {
  const _$Car();

  String get name;
  String get manufacturer;
  double get price;
  bool operator ==(other) {
    if (identical(this, other)) return true;
    if (other is! Car) return false;

    return true && == &&
        this.manufacturer == other.manufacturer &&
        this.price == other.price;

  int get hashCode {
    return mapPropsToHashCode([name, manufacturer, price]);

  String toString() {
    return 'Car <\'name\': ${},\'manufacturer\': ${this.manufacturer},\'price\': ${this.price},>';

  Car copyWith({String name, String manufacturer, double price}) {
    return Car(
      name: name ??,
      manufacturer: manufacturer ?? this.manufacturer,
      price: price ?? this.price,

3. Extend class with generated base class #

class Car extends _$Car {
  final String name;
  final String manufacturer;
  final double price;

  Car({, this.manufacturer, this.price});

Collection equality

class Car extends _$Car {
  @Collection(deepEquality: true) // Short-hand: @Collection()
  final List<String> parts;

  const Car({});


  1. Why you didn't use extension methods?

As the docs says that:

It is a compile-time error if an extension:

  • Declares a member with the same basename as a member declared by Object (==, hashCode, toString, noSuchMethod, runtimeType). This applies to both static and instance member declarations.
  1. May I use generics?


[0.3.0+1] #

  • Copy README from repository to dataclass directory

[0.3.0] - 2019/01/19 #

  • Add @Collection() annotation to use DeepCollectionEquality from collection package to compare collections
  • Add @dataClass shorthand for @DataClass()

[0.2.0] - 2019/12/25 #

  • enable usage of generic types, eg. Product<T>
  • BREAKING CHANGE: change generated class name from _Class to _$Class
  • move example to dataclass package directory

[0.1.0+1] - 2019/12/22 #

  • update README - badges

[0.1.0] - 2019/12/22 #

  • @DataClass() annotation used by dataclass_generator
  • mapPropsToHashCode from equatable package used by hashCode getter


dataclass_example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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

  dataclass: ^0.3.0+1

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:dataclass/dataclass.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 Feb 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
collection >=1.14.0 <1.15.0 1.14.12