copy_with_extension_gen 1.0.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 83

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

1.0.8 Analyzer rules #

  • Suppresses some of the analyzer's rules as we do not support generic types yet.

1.0.7 Extension name fix #

  • Creates a unique extension name for each class.

1.0.6 Minor corrections #

  • Minor metadata and description corractions.

1.0.0 Initial release #

  • Lets you generate a copyWith extension for objects annoteted with @CopyWith().

example/example.dart

import 'package:meta/meta.dart' show immutable;
import 'package:copy_with_extension/copy_with_extension.dart';

/// Make sure that `part` is specified, even before launching the builder
part 'example.g.dart';

@immutable
@CopyWith()
class SimpleObject {
  final String id;
  final int value;

  /// Make sure that constructor has named parameters (wrapped in curly braces)
  SimpleObject({this.id, this.value});
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  copy_with_extension_gen: ^1.0.8

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:copy_with_extension_gen/builder.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
66
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

We analyzed this package on Mar 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/copy_with_generator.dart. (-0.50 points)

Analysis of lib/src/copy_with_generator.dart reported 1 hint:

line 76 col 34: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
analyzer >=0.39.0 <1.0.0 0.39.4
build >=1.0.0 <2.0.0 1.2.2
copy_with_extension >=1.0.0 <2.0.0 1.0.7
logging >=0.0.0 <1.0.0 0.11.4
source_gen >=0.9.0 <1.0.0 0.9.5
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.6.0
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.9.3
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+14
yaml 2.2.0
Dev dependencies
build_runner >=1.0.0 <2.0.0
build_test <1.0.0
test <2.0.0