Freezed class
A data-class macro.
Warning: Macros are still an experimental feature of Dart. Lots of features are missing, so expect bugs.
This macro generates a toString
, copyWith
, ==
, and hashCode
implementation for a class.
Note:
copyWith
supper
class.copyWith(field: null). This will clone the object with
fieldset to
null`.
hashCode
, ==
and toString
won't be generated if the class
already possesses an implementation of those methods.
Two possible syntaxes are supported:
-
Constructor-first. You define a class with a constructor, and the macro will generate fields for you. This gives fine-grained control over positional vs named parameters. It offers the ability to use asserts,
super
, ... Although macros have still lots of things related to this that don't work. So although the syntax is very flexible, lots of things are not supported yet. -
Field-first. You define a class with fields, and the macro will generate a constructor for you. This is the most stable syntax. But it is less flexible, and not very dart-like. If you want full control over constructors, you'll have to use the constructor-first syntax.
Constructor-first usage
To use the constructor-first syntax, define a class with a constructor, but no fields:
@Freezed()
class Example {
Example({required int foo, required String bar});
}
The macro will generate the fields for you, along with the various methods.
void main() {
final example = Example(foo: 42, bar: '42');
print(example.foo); // 42
}
Using named constructors.
Naturally, the macro expects that you use the default constructor.
If you wish to generate fields based on a named constructor instead,
specify @Freezed(constructor: '<constructor name>')
.
@Freezed(constructor: 'custom')
class Example {
Example.custom({required int foo, required String bar});
}
Field-first usage
To use the field-first syntax, define a class with fields but default constructor. The macro will then generate a constructor for you, along with the various methods.
@Freezed()
class Example {
final int foo;
final String bar;
}
Currently, all fields are "named" parameters, and they are optional if they are nullable.
void main() {
final example = Example(foo: 42, bar: '42');
print(example.foo); // 42
}
Specifying the constructor name
If you wish to generate a non-default constructor,
you can specify @Freezed(constructor: '<constructor name>')
.
@Freezed(constructor: 'custom')
class Example {
final int foo;
}
void main() {
final example = Example.custom(foo: 42);
}
Constructors
Properties
- constructor → String?
-
The name of the constructor to use.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- makeCollectionsUnmodifiable → bool
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- superCtor → String?
-
The name of the
super
constructor to call.final
Methods
-
buildDeclarationsForClass(
ClassDeclaration clazz, MemberDeclarationBuilder builder) → FutureOr< void> -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited