struct_annotation 0.0.0-dev.3
struct_annotation: ^0.0.0-dev.3 copied to clipboard
Experimental support for data classes in Dart using pkg:macros
struct_annotation #
π§ Experimental π§
Experimental support for data classes in Dart using macros.
β¨ Features #
πͺ¨ const constructors with required, named parameters
π¨οΈ copyWith with optional, nullable, named parameters
β¨ toString for an improved string representation
β―οΈ operator== and hashCode for value equality
π§βπ» Example #
import 'package:struct_annotation/struct_annotation.dart';
@Struct()
class Person {
final String name;
final int age;
}
void main() {
// πͺ¨ Create a const instance with required, name parameters.
const jane = Person(name: 'Jane', age: 42);
// π¨οΈ Create copies of your object.
final john = jane.copyWith(name: 'John');
// β¨ Human-readable string representation.
print(jane); // Person(name: Jane, age: 42)
print(john); // Person(name: John, age: 42)
// β―οΈ Value equality comparisons.
print(jane == jane.copyWith()); // true
print(john == john.copyWith(age: 21)); // false
}
π Quick Start #
-
Switch to the Flutter
masterchannelflutter channel master -
Add
package:struct_annotationto yourpubspec.yamldependencies: struct_annotation: ^0.0.0-dev.1 -
Enable experimental macros in
analysis_options.yamlanalyzer: enable-experiment: - macros -
Use the
@Structannotation (see above example). -
Run it
dart --enable-experiment=macros run main.dart
*Requires Dart SDK >= 3.5.0-152.0.dev