data_class 0.1.2 copy "data_class: ^0.1.2" to clipboard
data_class: ^0.1.2 copied to clipboard

Experimental support for data classes in Dart using pkg:macros

data_class #

build pub package License: MIT

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: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
}

🚀 Quick Start #

  1. Add package:data_class to your pubspec.yaml

    dependencies:
      data_class: any
    
  2. Enable experimental macros in analysis_options.yaml

    analyzer:
      enable-experiment:
        - macros
    
  3. Use the @Data annotation (see above example).

  4. Run it

    dart --enable-experiment=macros run main.dart
    

*Requires Dart SDK >= 3.5.0

50
likes
150
points
119
downloads

Publisher

verified publisherfelangel.dev

Weekly Downloads

Experimental support for data classes in Dart using pkg:macros

Repository (GitHub)
View/report issues

Topics

#macros #data-class

Documentation

API reference

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

collection, equatable, macros

More

Packages that depend on data_class