equatable_macro (Pre-release: 0.0.1-main.1)

A Dart package providing a macro to automatically make classes equatable, inspired by the equatable package. This is a pre-release version and may contain experimental features.

Features

  • Automatically implements equality and hashCode for your classes.
  • Easy to use with minimal boilerplate.
  • Generates a toString() method to provide a string representation of your objects.

Installation

Add the following to your pubspec.yaml:

dependencies:
  equatable_macro: ^0.0.1-main.1

Add the following to your analysis_options.yaml:

analyzer:
  enable-experiment:
    - macros

Usage

Import the package and use the @Equatable() annotation:

import 'package:equatable_macro/equatable_macro.dart';

@Equatable() //or @Equatable(stringify: true)
class Example {
  final int id;
  final String name;

  const Example(this.id, this.name);
}

Example

// lib/main.dart
import 'package:equatable_macro/equatable_macro.dart';

void main() {
  var a = Example(1, 'a');
  var b = Example(1, 'a');
  var c = Example(2, 'b');

  print(a == b); // true
  print(a == c); // false
}

@Equatable() //or @Equatable(stringify: true)
class Example {
  final int id;
  final String name;

  const Example(this.id, this.name);
}

Run

dart --enable-experiment=macros run lib/main.dart
@Equatable(stringify: true) macro applied to a class Demonstration of the @Equatable(stringify: true) macro in use
Screenshot 1 Screenshot 2

Benchmarks

Benchmarks for this package are yet to be added. Stay tuned for updates.

Special Thanks

Contributing

Contributions are welcome! Please submit pull requests or create issues for any improvements or bugs you find.

Github Repo

If you find this project helpful, consider sponsoring it on GitHub:

Github Sponsor

Follow on YouTube For more tutorials and project walkthroughs, subscribe to our YouTube channel:

@Antinna YouTube Channel


Maintained by Manish Gautam



Powered By Antinna

Libraries

equatable_macro
Support for doing something awesome.