Last updated:
0 purchases
mek data class
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
Features #
Auto generation of:
✅ Inheritance and generic classes supported
✅ hashCode and == methods
✅ pretty toString method
✅ copyWith method
✅ *Changes class and change, toChanges methods in data class
✅ *Builder class to build your class
Install package #
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:
copied to clipboard
Run the 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';
copied to clipboard
Usage/Examples #
You can see some examples in
basic
generics
inheritance
Basic #
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';
}
copied to clipboard
Customization of the equal operator and hashcode through the use of the Equality class is supported. See example.
Inheritance #
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';
}
copied to clipboard
Generics #
You can also declare classes with generic types
@DataClass()
class Value<T> with _$Value<T> {
final T value;
const Product({
required this.value,
});
}
copied to clipboard
Pretty string #
Use the ClassToString package to perform the toString method
final product = Product(...);
/// Product(
/// title=Overlord,
/// price=12,
/// )
print(product);
copied to clipboard
CopyWith #
The classic copyWith, need explanations? No, but try to prefer using *Changes which supports nullability
final product = Product(...);
print(product.copyWith(title: 'Raisekamika'));
copied to clipboard
Enable in build.yaml with copyable: true
*Changes #
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();
copied to clipboard
Enable in build.yaml with changeable: true
*Changes.update
Update the *Changes class by passing a function
final updatedChanges = changes.update((c) => c..title = 'Albedo'); // title=Albedo
copied to clipboard
*Changes.replace
Update the properties of the *Changes class with the properties of the DataClass
final updatedChanges = productChanges.replace(product); // title=Raisekamika
copied to clipboard
*Changes.build
Build the DataClass from *Changes class
Product product = productChanges.build();
copied to clipboard
*Builder #
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();
copied to clipboard
DataClassFields #
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';
}
copied to clipboard
Global Configs #
See the docs of the DataClass class for more information
Key
Default
Description
page_width
80
adjust the page formatting width of the generated dart code
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:data_class:
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
copied to clipboard
Recommended options #
comparable: true
stringify: true
changeable: true
copied to clipboard
Motivations #
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
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.