struct_annotation 0.0.0-dev.5
struct_annotation: ^0.0.0-dev.5 copied to clipboard
Experimental support for data classes in Dart using pkg:macros
struct_annotation #
🚧 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;
}
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
}
🚀 Quick Start #
-
Switch to the Flutter
master
channelflutter channel master
-
Add
package:struct_annotation
to yourpubspec.yaml
dependencies: struct_annotation: ^0.0.0-dev.1
-
Enable experimental macros in
analysis_options.yaml
analyzer: enable-experiment: - macros
-
Use the
@Struct
annotation (see above example). -
Run it
dart --enable-experiment=macros run main.dart
*Requires Dart SDK >= 3.5.0-152.0.dev