Skip to content
pubscout

maybe_copy_with

1.0.0, released a year ago by an unverified publisher

Write ergonomic copyWith methods for immutable models without build_runner.

WASM-ready
$ flutter pub add maybe_copy_with
Source repository

What it does

Provides a mixin and extension methods to distinguish between keeping a nullable field unchanged and explicitly setting it to null in copyWith methods. Operates entirely in pure Dart without code generation.

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

Use it when

  • You want manual copyWith methods without dealing with build_runner or code generation.
  • You need a clean way to differentiate between omitting a nullable field and setting it to null.

Skip it when

  • You prefer automated code generation for your immutable models like freezed or built_value.
  • Your codebase has a massive number of models and manual boilerplate is strictly prohibited.

Strengths

  • Zero dependencies and lightweight.
  • No code generation or build_runner setup required.
  • Solves the nullability dilemma in copyWith methods cleanly.

Weaknesses

  • Fields must still be manually listed and implemented in copyWith.
  • Requires wrapping nullable field arguments with .maybe().

Setup

Kind
Pure Dart
Effort
Low
Code generation
Yes (build_runner)
Direct dependencies
0
class User with MaybeCopyWith<User> {
  final String name;
  final String? gender;

  const User({required this.name, this.gender});

  @override
  User copyWith({
    String? name,
    Maybe<String>? gender,
  }) {
    return User(
      name: name ?? this.name,
      gender: gender.or(this.gender),
    );
  }
}

Alternatives

Packages that do the same job and could replace maybe_copy_with.

  • freezed

    If you want fully automated code generation for immutable classes and copyWith methods instead of writing them manually.

  • built_value

    If you prefer a strict, builder-based approach to immutability and code generation.