The purpose of this library is to expose the generation of very simple class methods with excellent performance and little code generation. For this you will only be able to enable what you need of the supported features
Auto generation of:
- Inheritance and generic classes supported
-
hashCode
and==
methods - pretty
toString
method -
copyWith
method -
*Changes
class andchange
,toChanges
methods in data class -
*Builder
class to build your class
To use DataClass, you will need your typical build_runner/code-generator setup. First, install build_runner, data_class, data_class_generator by adding them to your pubspec.yaml file:
# pubspec.yaml
dependencies:
mek_data_class:
dev_dependencies:
build_runner:
mek_data_class_generator:
To run the code generator you can use:
<dart|flutter> pub run build_runner build
As such, a file that wants to use DataClass will start with:
import 'package:mek_data_class/mek_data_class.dart';
part 'my_file.g.dart';
You can see some examples in
Because the boiler plate is generated as a mixin, it is minimally intrusive on the interface of the class. You only have to provide a constructor with named arguments for all fields and extend the generated mixin.
@DataClass()
class Product with _$Product {
final String title;
@DataField(equality: DefaultEquality())
final double price;
const Product({
required this.title,
required this.price,
});
String get titlePrice => '$title$price';
}
Customization of the equal operator and hashcode through the use of the Equality class is supported. See example.
Taking into consideration the previous example you can write and inherit all methods
@DataClass()
class PrettyProduct extends Product with _$PrettyProduct {
final String color;
const Product({
required String title,
required double price,
required this.color,
}) : super(title: title, price: price);
String get titlePriceColor => '$titlePrice$color';
}
You can also declare classes with generic types
@DataClass()
class Value<T> with _$Value<T> {
final T value;
const Product({
required this.value,
});
}
Use the ClassToString package to perform the toString
method
final product = Product(...);
/// Product(
/// title=Overlord,
/// price=12,
/// )
print(product);
The classic copyWith, need explanations? No, but try to prefer using *Changes
which supports nullability
final product = Product(...);
print(product.copyWith(title: 'Raisekamika'));
Enable in
build.yaml
withcopyable: true
Unlike a builder you cannot set values to null but the field is not defined as such and cannot be instantiated
final updatedProduct = product.change((changes) => changes..title = 'Raisekamika');
final changes = product.toChanges();
changes.title = 'Raisekamika';
final updatedProduct = changes.build();
Enable in
build.yaml
withchangeable: true
Update the *Changes
class by passing a function
final updatedChanges = changes.update((c) => c..title = 'Albedo'); // title=Albedo
Update the properties of the *Changes
class with the properties of the DataClass
final updatedChanges = productChanges.replace(product); // title=Raisekamika
Build the DataClass
from *Changes
class
Product product = productChanges.build();
Build your class using a builder. It is not safe to construct a class using a builder but it allows you to complete the construction whenever you want.
@DataClass(buildable: true)
class Product with _$Product {
final int id;
const Product({required this.id});
factory Product.build(void Function(ProductBuilder b) updates) =>
(ProductBuilder().update(updates)).build();
}
final builder = ProductBuilder();
builder.id = 12;
final product = builder.build();
Generate a class that contains the names of the fields of the Data Class. @DataClass(createFieldsClass)
Example:
@DataClass(createFieldsClass: true)
class Product with _$Product {
final int id;
const Product({required this.id});
}
// GENERATED CODE
class ProductFields {
final String _path;
const ProductFields([this._path = '']);
String get id => '${_path}id';
}
See the docs of the DataClass class for more information
Key | Default | Description |
---|---|---|
stringify_if_null | true |
if set to false , null values will not be included in the toString |
stringify_type | params |
if set to fields , fields of a class that are not passed to the constructor will also be included |
fields_class_visible | true |
if set to false , the fields classes is private |
# build.yaml
targets:
$default:
builders:
mek_data_class_generator:
enabled: true
options:
page_width: 80
comparable: true
stringify: true
stringify_type: params | fields
stringify_if_null: true
buildable: false
copyable: false
changeable: false
changes_visible: false
create_fields_class: false
fields_class_visible: true
comparable: true
stringify: true
changeable: true
- Some packages generate a lot of code and mess with the normal, classic class construction in dart. Also you can't easily select classes without any problem, without having to create mixins for the methods
- Some packages require you to mark all your fields with @override
Similar packages freezed, built_value, dataclass_beta, functional_data