easy_copy_with 4.0.0
easy_copy_with: ^4.0.0 copied to clipboard
Code generator for automatic copyWith methods in Dart classes using @CopyWith annotation.
easy_copy_with #
Code generator for automatic copyWith methods in Dart classes using the @CopyWith annotation from easy_copy_with_annotation.
Installation #
dependencies:
easy_copy_with_annotation: ^1.0.0
dev_dependencies:
easy_copy_with: ^4.0.0
build_runner: ^2.10.4
Quick Start #
import 'package:easy_copy_with_annotation/easy_copy_with_annotation.dart';
part 'user.g.dart';
@CopyWith()
class User {
final String name;
final int age;
final String? email;
const User({required this.name, required this.age, this.email});
}
Generate code:
dart run build_runner build
Use:
final user = User(name: 'John', age: 30);
final olderUser = user.copyWith(age: 31);
final withoutEmail = user.copyWith(email: null); // nullable fields supported
Features #
- Annotation-based API
- Build-time code generation (no runtime overhead)
- Supports nullable and non-nullable fields
- Supports generic type parameters
- Supports sealed classes
- Supports named constructors
Sealed Classes #
@CopyWith()
sealed class Role {
const factory Role.admin({required int adminLevel}) = Admin;
const factory Role.userRole({required String username}) = UserRole;
const Role._();
}
final class Admin extends Role {
final int adminLevel;
const Admin({required this.adminLevel}) : super._();
}
final class UserRole extends Role {
final String username;
const UserRole({required this.username}) : super._();
}
Migration from 3.x #
- Add
easy_copy_with_annotation: ^1.0.0todependencies - Move
easy_copy_withtodev_dependencies - Replace imports:
import 'package:easy_copy_with/annotations.dart'->import 'package:easy_copy_with_annotation/easy_copy_with_annotation.dart'import 'package:easy_copy_with/easy_copy_with.dart'->import 'package:easy_copy_with_annotation/easy_copy_with_annotation.dart'
License #
MIT