dart_object_extension

pub

This plugin is a dart object extension plugin.

Features

  • CopyWith (nullable available)

Setup

Set the following in pubspec.yaml

dependencies:
  ...
  dart_object_extension: latest

dev_dependencies:
  ...
  build_runner: ^2.1.11
  dart_object_extension_gen: latest

Annotation Example

CopyWith

for example, create a stduent.dart file.

import 'package:dart_object_extension/dart_object_extension.dart';

part 'person.g.dart';

@CopyWith()
class Person {
  final int id;
  final String name;
  final int? age;

  const Person({
    required this.id,
    required this.name,
    this.age,
  });
}

Run code generation

flutter pub run build_runner build

copywith extension uses Functional parameters. A null check is also possible.

  • Basic Example
const person = Person(id: 0, name: 'Jin');
final personOther = person.copyWith(
  name: () => 'Sugar',
  age: () => 25,
);
  • Compile Error Example (name is not nullable)
const person = Person(id: 0, name: 'Jin');
final personOther = person.copyWith(
  name: () => null, // compile error
);
  • Compile Pass Example (age is nullable)
const person = Person(id: 0, name: 'Jin');
final personOther = person.copyWith(
  age: () => null, // compile pass
);