easy_copy_with 1.0.0 copy "easy_copy_with: ^1.0.0" to clipboard
easy_copy_with: ^1.0.0 copied to clipboard

Package for automatic generation of copyWith methods in Dart classes using @CopyWith annotation.

Easy CopyWith #

pub package License

A lightweight Dart package for automatic generation of copyWith methods using the @CopyWith annotation.

Features #

  • 🚀 Simple annotation-based API
  • ⚡ Build-time code generation (no runtime overhead)
  • 🔄 Supports nullable and non-nullable fields
  • 🎯 Compatible with freezed patterns
  • 📦 Minimal dependencies
  • 🛠️ Easy integration with existing projects

Installation #

Add to your pubspec.yaml:

dependencies:
  easy_copy_with: ^1.0.0

dev_dependencies:
  build_runner: ^2.3.3

Then run:

dart pub get

Quick Start #

  1. Import the package:
import 'package:easy_copy_with/easy_copy_with.dart';
  1. Add part directive:
part 'your_file.copy_with.g.dart';
  1. Annotate your class:
@CopyWith()
class Person {
  final String name;
  final int age;
  final String? email;

  const Person({
    required this.name,
    required this.age,
    this.email,
  });
}
  1. Generate code:
dart run build_runner build
  1. Use the generated copyWith method:
final person = Person(name: 'John', age: 30);
final olderPerson = person.copyWith(age: 31);

Example #

import 'package:easy_copy_with/easy_copy_with.dart';

part 'user.copy_with.g.dart';

@CopyWith()
class User {
  final String name;
  final int age;
  final String? email;
  final bool isActive;

  const User({
    required this.name,
    required this.age,
    this.email,
    this.isActive = true,
  });
}

void main() {
  final user = User(name: 'Alice', age: 25);

  // Create copy with single field change
  final updatedUser = user.copyWith(age: 26);

  // Create copy with multiple field changes
  final activeUser = user.copyWith(
    email: '[email protected]',
    isActive: true,
  );

  print('Original: ${user.name}, ${user.age}');
  print('Updated: ${updatedUser.name}, ${updatedUser.age}');
}

Generated Code #

The package uses source_gen and build_runner to analyze classes with the @CopyWith annotation and generate extension methods at build time.

For the example above, the generated code looks like:

extension UserCopyWith on User {
  User copyWith({
    String? name,
    int? age,
    String? email,
    bool? isActive,
  }) {
    return User(
      name: name ?? this.name,
      age: age ?? this.age,
      email: email ?? this.email,
      isActive: isActive ?? this.isActive,
    );
  }
}

Commands #

# Generate code once
dart run build_runner build

# Watch for changes and rebuild automatically
dart run build_runner watch

# Clean generated files
dart run build_runner clean

Requirements #

  • Dart SDK: >=3.9.2
  • Compatible with Flutter projects

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

License #

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

3
likes
150
points
249
downloads

Publisher

unverified uploader

Weekly Downloads

Package for automatic generation of copyWith methods in Dart classes using @CopyWith annotation.

Repository (GitHub)
View/report issues

Topics

#codegen #copy-with #code-generation #annotation

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, build, meta, source_gen

More

Packages that depend on easy_copy_with