Support for data classes in Dart using macros.
πͺ¨ const
constructors with required, named parameters
π¨οΈ copyWith
with optional, nullable, named parameters
β¨ toString
for an improved string representation
β―οΈ operator==
and hashCode
for value equality
import 'package:data_class/data_class.dart';
@Data()
class Person {
final String name;
}
void main() {
// πͺ¨ Create a const instance with required, name parameters.
const dash = Person(name: 'Dash');
// π¨οΈ Create copies of your object.
final sparky = dash.copyWith(name: 'Sparky');
// β¨ Human-readable string representation.
print(dash); // Person(name: Dash)
print(sparky); // Person(name: Sparky)
// β―οΈ Value equality comparisons.
print(dash == dash.copyWith()); // true
print(dash == sparky); // false
}
-
Add
package:data_class
to yourpubspec.yaml
dependencies: data_class: any
-
Enable experimental macros in
analysis_options.yaml
analyzer: enable-experiment: - macros
-
Use the
@Data
annotation (see above example). -
Run it
dart --enable-experiment=macros run main.dart
*Requires Dart SDK >= 3.5.0