auto_data 0.0.3 auto_data: ^0.0.3 copied to clipboard
Automatically generate simple data class files for Dart. This is the runtime dependency.
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)
- ✅ Custom constructors should be copied over (Issue #1)
- ✅ Default values by assigning during declaration:
String name = 'Paul';
- ✅ Add @nullable annotation for fields that are not required
- ❌ Deep immutability for Map
- ✅ Deep immutability for List
- ✅ Serialization toMap/fromMap
- ✅ 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.