auto_data_generator 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

auto_data #

Generate simple data classes for Dart.

A data class is an immutable class meant to hold data, similar to Kotlin's data class.

Example #

Specify the data class using the @data annotation:

@data
class $Point {
  double x;
  double y;
}

Enjoy your generated named constructor, ==/hashCode, toString, copyWith, and serialization:

// GENERATED CODE - DO NOT MODIFY BY HAND
@immutable
class Point {
  final double x;
  final double y;

  const Point({
    @required this.x,
    @required this.y,
  });

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Point &&
          runtimeType == other.runtimeType &&
          x == other.x &&
          y == other.y;

  @override
  int get hashCode => x.hashCode ^ y.hashCode;

  @override
  String toString() {
    return 'Point{x: ' + x.toString() + ', y: ' + y.toString() + '}';
  }

  Point copyWith({
    double x,
    double y,
  }) {
    return Point(
      x: x ?? this.x,
      y: y ?? this.y,
    );
  }

  Point.fromMap(Map<String, dynamic> m)
      : x = m['x'],
        y = m['y'];

  Map<String, dynamic> toMap() => {'x': x, 'y': y};

  factory Point.fromJson(String json) => Point.fromMap(jsonDecode(json));

  String toJson() => jsonEncode(toMap());
}

Requirements #

Add the following to your pubspec.yaml:

dependencies:
  auto_data: ^0.0.3

dev_dependencies:
  build_runner: ^1.0.0
  auto_data_generator: ^0.0.3

Create your point.dart file with correct imports:

import 'package:meta/meta.dart';
import 'package:collection/collection.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';

part 'point.g.dart';

@data
class $Point {
  double x;
  double y;
}

Lastly, generate using build_runner:

pub run build_runner build

or

pub run build_runner watch

Use your generated Point class:

import 'point.dart';

final p1 = Point(x: 0, y: 1);
final p2 = Point(x: 0, y: 2);
assert(p1 != p2);
final p3 = p1.copyWith(y: 2);
assert(p2 == p3);
print(p3.toString());

Advanced usage #

import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';
import 'foo.dart';

part 'person.g.dart';

/// All comments are copied to generated code
@data
class $Person {
  /// This field gets a default value
  String name = 'Paul';

  /// This field is not required
  @nullable
  double weight;

  /// Age of the person
  int age;

  /// Depend on another generated class
  $Foo foo;

  /// Deep comparison of lists
  List<$Person> friends;

  /// Custom constructors are copied over
  $Person.genius()
  : name = 'Albert',
    age = 140;
}

Todo's #

  • [ ] Optional constructor types (named, private, const, etc)
  • [x] Custom constructors should be copied over (Issue #1)
  • [x] Default values by assigning during declaration: String name = 'Paul';
  • [x] Add @nullable annotation for fields that are not required
  • [ ] Deep immutability for Map
  • [x] Deep immutability for List
  • [x] Serialization toMap/fromMap
  • [x] Serialization toJson/fromJson

Limitations #

Cannot set values to null with copyWith #

Ex) Clearing a user's avatar image:

profile = profile.copyWith(imageUrl: null); // This won't have an effect since copyWith ignores null input parameters.

References #

  1. Issue: Statically tracked shared immutable objects
  2. Proposal: Shared immutable objects
  3. Patterns and related features

0.0.3 #

  • Added support for toJson and fromJson
  • Renamed toFirebaseMap/fromFirebaseMap -> toMap/fromMap

0.0.2 #

  • Various package structure fixes

[0.0.1] - July 24, 2019.

  • Initial release.

example/lib/main.dart

import 'package:example/foo.dart';
import 'bar.dart';

void main() {
  final foo = new Foo();
  final bar = new Bar(foo: foo);
}

Use this package as a library

1. Depend on it

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


dependencies:
  auto_data_generator: ^0.0.3

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:auto_data_generator/auto_data_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
23
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: other

Primary library: package:auto_data_generator/auto_data_generator.dart with components: io, build, mirrors.

Health issues and suggestions

Document public APIs. (-0.25 points)

38 out of 39 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/auto_data_generator.dart. (-1.49 points)

Analysis of lib/src/auto_data_generator.dart reported 3 hints:

line 94 col 3: This function has a return type of 'T', but doesn't end with a return statement.

line 100 col 3: This function has a return type of 'T', but doesn't end with a return statement.

line 124 col 9: Use isEmpty instead of length

Format lib/src/file_generator.dart.

Run dartfmt to format lib/src/file_generator.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
auto_data ^0.0.3 0.0.3
build ^1.0.0 1.2.0
source_gen ^0.9.4+1 0.9.4+5
Transitive dependencies
analyzer 0.38.5
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
dart_style 1.3.1
front_end 0.1.27
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.27
logging 0.11.3+2
meta 1.1.7
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.2.0
Dev dependencies
build_runner ^1.0.0
test any