Skip to content

Field Annotation

Efra Espada edited this page Jan 30, 2024 · 1 revision

The @Field annotation lets you easily de/serialize Dart instances. It offers different parameters:

String name

@Field(
  name: 'some_key',
)

The key for de/serialization.

bool primary

@Field(
  primary: true,
)

Sets the current field as the primary key to differentiate that instance from others.

bool basic

@Field(
  basic: true,
)

Sets if the current field should have a basic de/serialization.

toJson()

'field': instance['field']

fromJson()

instance['field'] = json['field']

bool recycle

@Field(
  recycle: true,
)

Defines the current field for being recycled on the deserialization process. Instead of creating a new instance of the field, the library will use the old one, to avoid memory consumption.

This field only applies to complex instances, not to primitive types.

String import

@Field(
  import: 'package:my_app_package/model/other_class.dart',
)

Helps de/serialize instances whose classes are not managed by the library.

DefaultValue? defaultValue

@Field(
  defaultValue: DefaultValue(
    value: 3 | true | 'string',
  ),
)