draft 0.0.12
draft: ^0.0.12 copied to clipboard
Immer, but for dart. Convert between immutable and mutable objects.
Draft is immer, but for dart.
Convert any object into a mutable draft, modify it, then convert it back to the immutable version. All while using a comfy API :)
import 'package:draft/draft.dart';
part 'example.draft.dart';
@draft
class Foo {
final int value;
const Foo(this.value);
}
// Foo is immutable, but we can edit it with Draft
Foo foo = Foo(1).produce((draft) {
draft.value += 1;
});
// prints 2
print(foo.value);
See the examples directory for more info.
Set up #
First, install it
dart pub add dev:build_runner dev:draft_builder draft
Next, define your drafts
import 'package:draft/draft.dart';
part 'example.draft.dart';
@draft
class Foo {
final int value;
const Foo({
required this.value,
});
}
Then run the build runner
dart run build_runner build --delete-conflicting-outputs
Now you can use your drafts!
Foo(value: 1).produce((draft) {
draft.value += 1;
});
Use cases #
Draft supports a variety of use cases and does its best to use native language features to make it easier to use.
The produce
method #
The produce method exposes a callback that lets you modify a mutable version of the object and returns a new immutable instance.
final Foo foo = Foo(1).produce((draft) {
draft.value += 1;
});
The draft
method #
If you prefer, you can create a draft from your immutable object and modify it direct. When you're done editing it, call save()
to get a new immutable instance.
// The original immutable object
final original = Foo(1, 2);
// Create and modify a draft
final draft = original.draft()
..value1 += 1
..value2 += 5;
// Convert it back into an immutable object
Foo foo = draft.save();
Nested drafts #
You can nest draftable classes inside of other draftable classes. Draft is smart and lets you modify the nested drafts.
@draft
class A {
final B b;
A(this.b);
}
@draft
class B {
final int value;
B(this.value);
}
final a = A(B(1)).produce((draft) {
draft.b.value += 1;
});
Collections #
You can modify collections inline. Draft will create copies of the collection, without affecting the original.
@draft
class C {
final List<int> values;
C(this.values);
}
final c = C([1, 2, 3]).produce((draft) {
draft.values[0] += 1;
});
Equality #
Draft is unopinionated and does not provide any sort of equality checking out of the box. If you want equality checking, consider using equatable
Contributing #
If you like the package and want to contribute, feel free to open and issue or create a PR. I'm always open to suggestions and improvements.
keywords: flutter, dart, immutable, mutable, immer, draft