copy_with_annotation 0.0.2+5

  • Readme
  • Changelog
  • Installing
  • new63

copy_with_annotation #

This package generates a copyWith method using build_runner and json_serializable

Installation #

Add dependencies in your pubspec.yaml:



Usage #

In class you want to write toString() method:

  • Annotate the class with ToString()
  • Override the toString method.
import 'package:json_annotation/json_annotation.dart';
import 'package:copy_with_annotation/copy_with_annotation.dart';

part 'person.g.dart';

class Person {
  final String firstName;
  final String lastName;
  final DateTime dateOfBirth;

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
  Person copyWith(Map<String, dynamic> overrides) => _$PersonCopyWith(this, overrides);

Building creates the corresponding part person.g.dart:

part of 'person.dart';

Person _$PersonCopyWith(Person instance, Map<String, dynamic> overrides) {
  Map<String, dynamic> original = _$PersonToJson(instance);
  Map<String, dynamic> copy = <String, dynamic>{
    "firstName": overrides.containsKey("firstName")
        ? overrides["firstName"]?.toJson()
        : original["firstName"],
    "lastName": overrides.containsKey("lastName")
        ? overrides["lastName"]?.toJson()
        : original["lastName"],
    "dateOfBirth": overrides.containsKey("dateOfBirth")
        ? overrides["dateOfBirth"]?.toIso8601String()
        : original["dateOfBirth"],
  return _$PersonFromJson(copy);

Person _$PersonFromJson(Map<String, dynamic> json) {
  return Person(
    firstName: json['firstName'] as String,
    lastName: json['lastName'] as String,
    dateOfBirth: json['dateOfBirth'] == null
        ? null
        : DateTime.parse(json['dateOfBirth'] as String),

Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
      'firstName': instance.firstName,
      'lastName': instance.lastName,
      'dateOfBirth': instance.dateOfBirth?.toIso8601String(),

Features and bugs #

Please file feature requests and bugs at the Github Issue Tracker.

Github Issue tracker:

Author #

This CopyWith package is developed by The Next App. You can contact us at

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

Use this package as a library

1. Depend on it

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

  copy_with_annotation: ^0.0.2+5

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter 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_annotation/copy_with_annotation.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4

Health issues and suggestions

Document public APIs. (-1 points)

3 out of 3 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and copy_with_annotation.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0