felangel / data_class

Experimental support for data classes in Dart using macros.

Home Page:https://pub.dev/packages/data_class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

data_class

build pub package License: MIT

🚧 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: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. Switch to the Flutter master channel flutter channel master

  2. Add package:data_class to your pubspec.yaml

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

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

  5. Run it

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

*Requires Dart SDK >= 3.5.0-152.0.dev

About

Experimental support for data classes in Dart using macros.

https://pub.dev/packages/data_class

License:MIT License


Languages

Language:Dart 100.0%