equatable_gen 1.0.4
equatable_gen: ^1.0.4 copied to clipboard
Automatically creates 'props' getters for classes using the equatable package.
Package | Version |
---|---|
equatable_annotations |
|
equatable_gen |
Provides Dart Build System builder for generating List<Object?> _$props
private extensions for classes using Equatable.
Usage #
Setup #
-
Add to your
pubspec.yaml
:dependencies: equatable_annotations: ^latest_version dev_dependencies: equatable_gen: ^latest_version build_runner: ^latest_version
-
Create a
build.yaml
file in your project root:targets: $default: builders: equatable_gen: enabled: true options: auto_include: true
-
Add part declarations to your files:
part 'your_file_name.g.dart';
-
Run code generation:
flutter pub run build_runner build
Zero Annotation Approach #
With auto_include: true
in your build.yaml
, no annotations are needed. The generator automatically processes:
- Classes that extend
Equatable
- Classes that use
EquatableMixin
- Classes that inherit from a class using either of the above
All you need is:
class MyClass extends Equatable {
const MyClass({required this.id, required this.name});
final String id;
final String name;
@override
List<Object?> get props => _$props; // `_$props` will be automatically generated
}
The generator creates:
extension _$MyClassequatable_annotations on MyClass {
List<Object?> get _$props => [id, name];
}
Important
The props
will not be generated if the props
getter is not defined in the class.
Inheritance #
Inheritance works automatically. Fields from parent classes are included:
class ChildClass extends ParentClass { // ParentClass uses Equatable
const ChildClass({required this.name, required super.id});
final String name;
@override
List<Object?> get props => _$props;
}
// Generated:
extension _$ChildClassequatable_annotations on ChildClass {
List<Object?> get _$props => [name, id]; // Both fields included
}
Field/Getter Control #
Exclude Fields #
Exclude fields from props with @ignore
:
import 'package:equatable_annotations/equatable_annotations.dart';
@ignore
final String sensitiveData;
Include Getters #
Include getters in props with @include
:
import 'package:equatable_annotations/equatable_annotations.dart';
@include
String get fullName => '$firstName $lastName';
Advanced Configuration #
Customize behavior in your build.yaml
:
targets:
$default:
builders:
equatable_gen:
enabled: true
options:
auto_include: true # defaults to false
include: # Only include classes matching patterns (regex)
- User.*
exclude: # Exclude classes matching patterns (regex)
- .*ViewModel
include_getters: true # defaults to false
Manual Annotation (Alternative) #
If you prefer explicit control, you can use annotations:
import 'package:equatable_annotations/equatable_annotations.dart';
import 'package:equatable/equatable.dart';
part 'some_class.g.dart';
@generateProps
class SomeClass extends Equatable {
const SomeClass({required this.id});
final String id;
@override
List<Object?> get props => _$props;
}