Skip to content
pubscout

copy_with_extension_gen

18.0.1, released 5 days ago by alexander-kirsch.com

Generate type‑safe copyWith extensions for annotated classes and record typedefs.

$ flutter pub add copy_with_extension_gen
Source repository

What it does

It creates copyWith methods and optional copyWithNull helpers via the build system, allowing immutable objects to be cloned with modified fields. It also supports record typedefs and various customization options.

AI-written summary of version 18.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need immutable data classes with convenient copy methods
  • You prefer code generation over manual boilerplate
  • You want support for Dart records and null‑clearing

Skip it when

  • You already use a full‑featured codegen like freezed or built_value
  • You want runtime reflection instead of generated code

Strengths

  • Zero runtime cost, compile‑time safety
  • Supports Dart records and advanced options like immutable fields
  • No extra dependencies beyond build_runner

Weaknesses

  • Requires build_runner to regenerate code
  • Limited to annotated classes/records, not dynamic objects
  • Breaking changes in major releases (e.g., 17.0.0)

Setup

Kind
Pure Dart
Effort
Medium
Code generation
Yes (build_runner)
Direct dependencies
5

Add build_runner and copy_with_extension_gen to dev_dependencies, annotate classes with @CopyWith, run `dart run build_runner build`

```dart
import 'package:copy_with_extension/copy_with_extension.dart';

part 'basic_class.g.dart';

@CopyWith()
class BasicClass {
  final String id;
  final String? text;
  const BasicClass({required this.id, this.text});
}
```
```dart
final result = BasicClass(id: "id");
final copied = result.copyWith.text("test");
```

Alternatives

Packages that do the same job and could replace copy_with_extension_gen.

  • freezed_annotation

    Provides copyWith, immutability, and many other features in a single package, widely used in Flutter projects

Works with

Packages commonly used together with copy_with_extension_gen: ones it builds on, ones built on it, and its own family.