DataClass class

Adding this annotation to a class enables it to create common data class functionality

Available methods are

copyWith Generates a new instance of the class with optionally provide new fields values

If no value is provided (default), then true is assumed

YourClass copyWith(...)

hashAndEquals Implements hashCode and equals methods

If no value is provided (default), then true is assumed

@override
bool operator ==(Object other)

@override
int get hashCode

$toString Implements toString method

If no value is provided (default), then true is assumed

String toString()

fromJson Generates a factory constructor that creates a new instance from a Map

If no value is provided (default), then false is assumed

factory YourClass.fromJson(Map<dynamic, dynamic> json)

toJson Generates a function that coverts this instance to a Map

If no value is provided (default), then false is assumed

constructorName Name for the default constructor to be used

If no value is provided (default), then "ctor" will be used

Note: This is only available on file generation mode unmodifiableCollections Wraps collections (List, Map, Set) with the relative unmodifiable constructor.

Applies only for file generation mode

If no value is provided (default), then true is assumed

If set to false, then the fields of a collection type will be excluded from hashCode implementation so the hashCode result will be stable

@DataClass()
class User {
  User.ctor();
}
Annotations
  • @Target(<TargetKind>{TargetKind.classType})

Constructors

DataClass.new({bool? copyWith, bool? hashAndEquals, bool? $toString, bool? fromJson, bool? toJson, String? constructorName, bool? unmodifiableCollections})
Shorthand constructor
const

Properties

$toString bool?
If no value is provided (default), then true is assumed
final
constructorName String?
Default constructor name
final
copyWith bool?
If no value is provided (default), then true is assumed
final
fromJson bool?
If no value is provided (default), then false is assumed
final
hashAndEquals bool?
If no value is provided (default), then true is assumed
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toJson bool?
If no value is provided (default), then false is assumed
final
unmodifiableCollections bool?
If no value is provided (default), then true is assumed
final

Methods

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