copy_with_extension_gen 3.0.0 copy "copy_with_extension_gen: ^3.0.0" to clipboard
copy_with_extension_gen: ^3.0.0 copied to clipboard

SDKDart
outdated

Automatically generating `copyWith` extensions code for classes with `@CopyWith()` annotation.

Pub Package

Provides Dart Build System builder for generating copyWith extensions for classes annotated with copy_with_extension. For more information on how this package works, see my blog article.

This library allows you to copy instances of immutable classes modifying specific fields like so:

myInstance.copyWith.fieldName("test") // Preferred way with nullability support.

myInstance.copyWith(fieldName: "test", anotherField: "test") // Change multiple fields at once without nullability support.

myInstance.copyWithNull(fieldName: true, anotherField: true) // Nullify multiple fields at once.

Usage #

In your pubspec.yaml file:

  • Add to dependencies section copy_with_extension: ^3.0.0
  • Add to dev_dependencies section copy_with_extension_gen: ^3.0.0
  • Add to dev_dependencies section build_runner: ^2.1.7
  • Set environment to at least Dart 2.12.0 version like so: ">=2.12.0 <3.0.0"

Your pubspec.yaml should look like so:

name: project_name
description: project description
version: 1.0.0

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  ...
  copy_with_extension: ^3.0.0
  
dev_dependencies:
  ...
  build_runner: ^2.1.7
  copy_with_extension_gen: ^3.0.0

Annotate your class with CopyWith annotation:

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});
}

Make sure that you set the part file as in the example above part 'your_file_name.g.dart';.

Launch code generation:

flutter pub run build_runner build

Use

const result = BasicClass(id: "id")
final copied = result.copyWith.text("test") // Results in BasicClass(id: "id", text: "test");

Additional features #

Change several fields at once with copyWith()

You can modify multiple fields at once using copyWith as a function like so: myInstance.copyWith(fieldName: "test", anotherField: "test"). Be aware that this kind of usage does not support nullification and all passed null values will be ignored.

Nullifying instance fields:

The copyWith method ignores any null values that are passed to it. In order to nullify the class fields, an additional copyWithNull function can be generated. To achieve this, simply pass an additional parameter to your class annotation @CopyWith(generateCopyWithNull: true).

Immutable fields

If you want to prevent a particular field from modifying with copyWith method you can add an additional annotation like this:

@CopyWithField(immutable: true)
final int myImmutableField;

By adding this annotation you forcing your generated copyWith to always copy this field as it is, without allowing its modification.

179
likes
130
pub points
94%
popularity

Publisher

verified publisheroleksandrkirichenko.com

Automatically generating `copyWith` extensions code for classes with `@CopyWith()` annotation.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

analyzer, build, copy_with_extension, source_gen

More

Packages that depend on copy_with_extension_gen