CopyWithKey class

override the generation behavior of a field

  • Annotations on constructor parameters take precedence over fields
  • Annotations on fields are inherited in subclasses, while those on parameters do not Example:
@CopyWith(asExtension: false)
class Profile {
  final String firstname;
  @CopyWithKey(inCopyWith: false)
  final String lastname;

  final DateTime? birthday;

  const Profile({
    @CopyWithKey(inCopyWith: false)
    this.firstname = "",
    this.lastname = "",
    this.birthday
  });
}


@CopyWith(asExtension: true)
class UserProfile extends Profile {
  final String id;

  UserProfile({this.id = "", super.lastname, super.birthday, super.firstname});
}

This generates:

  • the lastname field is inherited 'as is', this also inherits its annotations.
  • the firstname field is also inherited 'as is', but the annotation is on the constructor parameter and therefor not inherited with the field.
extension $ProfileExtension on Profile {
  Profile copyWith({
    // String? firstname,
    // String? lastname,
    DateTime? birthday,
  }) {
    return Profile.new(
      // firstname: firstname ?? this.firstname,
      // lastname: lastname ?? this.lastname,
      birthday: birthday ?? this.birthday,
    );
  }
}

extension $UserProfileExtension on UserProfile {
  UserProfile copyWith({
    String? id,
    // String? lastname,
    DateTime? birthday,
    String? firstname,
  }) {
    return UserProfile.new(
      id: id ?? this.id,
      // lastname: lastname ?? this.lastname,
      birthday: birthday ?? this.birthday,
      firstname: firstname ?? this.firstname,
    );
  }
}
Annotations
  • @Target.new({TargetKind.field, TargetKind.parameter, TargetKind.optionalParameter, TargetKind.getter, TargetKind.overridableMember})

Constructors

CopyWithKey({bool? inCopyWith, String? field})
const

Properties

field String?
The field that should be used as the fallback in the copyWith() method
final
hashCode int
The hash code for this object.
no setterinherited
inCopyWith bool
if false, the field will not be included in the copyWith() method
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson() Map<String, Object?>
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited