dataclass_generator 0.3.0+2

  • Readme
  • Changelog
  • Installing
  • 69

Generator for dataclass package #

License Pub.dev Github Stars

@DataClass source generator used by dataclass package

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

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

Getting Started #

1. Annotate your class with @dataClass #

The class should:

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

  Car({this.name, 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.name == other.name &&
        this.manufacturer == other.manufacturer &&
        this.price == other.price;
  }

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

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

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

3. Extend class with generated base class #

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

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

Collection equality


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

  const Car({this.parts});
}

FAQ #

  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?

Yes.

[0.3.0+2] #

  • Remove debug prints

[0.3.0+1] #

  • Copy README from repository to dataclass_generator 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.2] - 2019/12/31 #

  • FIX not generating full delcation of colletion. eg. List get field instead List<myType> get field

[0.2.1] - 2019/12/26 #

  • FIX not generating base classes for @DataClass classes with no fields

[0.2.0] - 2019/12/25 #

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

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

  • update README - repo badges

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

  • add README

[0.1.0] - 2019/12/22 #

  • generating base class with equals, hashCode, toString, copyWith methods.

Use this package as a library

1. Depend on it

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


dependencies:
  dataclass_generator: ^0.3.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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_generator/dataclass_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Jun 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/dataclass_generator.dart. (-1.49 points)

Analysis of lib/dataclass_generator.dart reported 3 hints:

line 56 col 42: 'displayName' is deprecated and shouldn't be used. Use getDisplayString instead.

line 113 col 7: DO use curly braces for all flow control structures.

line 139 col 37: 'displayName' is deprecated and shouldn't be used. Use getDisplayString instead.

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (build, code_builder).

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and dataclass_generator.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.39.0 <0.40.0 0.39.10
build >=1.2.0 <1.3.0 1.2.2 1.3.0
code_builder >=3.2.0 <3.3.0 3.2.1 3.3.0
dart_style >=1.3.0 <1.4.0 1.3.6
dataclass >=0.3.0 <0.4.0 0.3.0+1
source_gen >=0.9.0 <0.10.0 0.9.5
Transitive dependencies
_fe_analyzer_shared 4.0.0
args 1.6.0
async 2.4.1
built_collection 4.3.2
built_value 7.1.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
fixnum 0.10.11
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
logging 0.11.4
matcher 0.12.6
meta 1.1.8
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.0
pub_semver 1.4.4
quiver 2.1.3
source_span 1.7.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
build_runner any
build_test any
test any