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

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 info on this package check out my blog article.

Usage #

In your pubspec.yaml file:

  • Add to dependencies section copy_with_extension: ">=1.0.0 <2.0.0"
  • Add to dev_dependencies section copy_with_extension_gen: ">=1.0.0 <2.0.0"
  • Add to dev_dependencies section build_runner: ">=1.0.0 <2.0.0"
  • Set environment to at least Dart 2.7.0 version like so: ">=2.7.0 <3.0.0"

Your pubspec.yaml should look like so:

name: project_name
description: project description
version: 1.0.0

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  ...
  copy_with_extension: ">=1.0.0 <2.0.0"
  
dev_dependencies:
  ...
  build_runner: ">=1.0.0 <2.0.0"
  copy_with_extension_gen: ">=1.0.0 <2.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;

  BasicClass({this.id});
}

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

The extension will be generated:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'basic_class.dart';

// **************************************************************************
// CopyWithGenerator
// **************************************************************************

extension CopyWithExtension on BasicClass {
  BasicClass copyWith({
    String id,
  }) {
    return BasicClass(
      id: id ?? this.id,
    );
  }
}

Launch code generation:

flutter pub run build_runner build

Nullifying the class 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).

179
likes
0
pub points
94%
popularity

Publisher

verified publisheroleksandrkirichenko.com

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

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, build, copy_with_extension, logging, source_gen

More

Packages that depend on copy_with_extension_gen