autoequal_gen 0.9.1 copy "autoequal_gen: ^0.9.1" to clipboard
autoequal_gen: ^0.9.1 copied to clipboard

Code generator which can simplify work with 'equatable'. It will generate 'List<Object?> props' private extensions for all annotated classes.

Pub Package

Provides Dart Build System builder for generating List<Object?> _$props private extensions for classes annotated with autoequal.

Usage #

In your pubspec.yaml file #

  • Add autoequal to your dependencies
  • Add autoequal_gen to your dev_dependencies
  • Add build_runner to your dev_dependencies

Annotate your class with @autoequal annotation #

import 'package:autoequal/autoequal.dart';
import 'package:equatable/equatable.dart';

part 'some_class.g.dart';

@autoequal
class SomeClass extends Equatable {
  const SomeClass({this.id, this.random});

  final String id;

  @override
  List<Object?> get props => _$props; //_$props will be generated
}

Make sure that you set the part file as in the example above part 'your_file_name.g.dart';.

Launch code generation

flutter pub run build_runner build

The extension will be generated

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'some_class.dart';

// **************************************************************************
// AutoequalGenerator
// **************************************************************************

extension _$SomeClassAutoequal on SomeClass {
  List<Object> get _$props => [id];
}

Auto Include with Annotating #

The @autoequal is smart enough to generate the props getter for classes that use Equatable without having to annotate the class with @autoequal.

To do this, update your build.yaml file to:

Include the auto_include: true option

targets:
  $default:
    builders:
      autoequal_gen:
        enabled: true
        options:
          auto_include: true
          exclude:
            - SomeClass # regex pattern (for class names)

Or, add a regex pattern to the include option

targets:
  $default:
    builders:
      autoequal_gen:
        enabled: true
        options:
          include:
            - SomeClass # regex pattern (for class names)

Then, use equatable and create the props getter as you normally would.

class SomeClass with EquatableMixin { // or `extends Equatable`
  const SomeClass({this.id});

  final String id;

  @override
  List<Object?> get props => _$props;
}

NOTE: If the props getter is not defined, the autoequal will not generate the _$props variable.

Inheritance #

If your class extends another class that uses Equatable, you can use the @autoequal annotation and autoequal will do the rest!

@autoequal
class SubClass extends BaseClass { // BaseClass extends Equatable
  const SubClass({required this.name, required super.id});

  final String name;


  @override
  List<Object?> get props => _$props;
}

// generated code
extension _$SubClassAutoequal on SubClass {
  List<Object?> get _$props => [name, id];
}

Field/Getter annotations #

Ignore #

You can include fields or getter methods in props by annotating them with @ignore.

@ignore
final int random;

Include #

You can include fields or getter methods in props by annotating them with @include.

@include
String get id => _id;

Build.yaml options #

By default, the builder will ignore all getter methods in props, but you can change this behavior by adding the following to your build.yaml file:

targets:
  $default:
    builders:

      autoequal_gen:
        enabled: true
        options:
          ... # view options below

Options #

To view a comprehensive list of options, view the source code

All options are optional. All option's keys are formatted in snake_case.

6
likes
130
points
1.42k
downloads

Publisher

verified publishermrgnhnt.com

Weekly Downloads

Code generator which can simplify work with 'equatable'. It will generate 'List<Object?> props' private extensions for all annotated classes.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, autoequal, build, change_case, code_builder, dart_style, meta, path, source_gen, source_span

More

Packages that depend on autoequal_gen