https://github.com/josiahsrc/draft
Immer, but for dart
https://github.com/josiahsrc/draft
Last synced: about 2 months ago
JSON representation
Immer, but for dart
- Host: GitHub
- URL: https://github.com/josiahsrc/draft
- Owner: josiahsrc
- License: mit
- Created: 2025-01-04T18:04:03.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-03T03:19:56.000Z (over 1 year ago)
- Last Synced: 2025-03-03T03:29:51.548Z (over 1 year ago)
- Language: Dart
- Size: 51.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Draft
Immer, but for dart. Convert between immutable and mutable objects.
:warning: This package is in its early stages and the API will change.
## Usage
Draft is inspired by immer.
```dart
import 'package:draft/draft.dart';
part 'example.draft.dart';
@draft
class Foo {
final int value;
const Foo({
required this.value,
});
}
void main() {
final foo1 = Foo(value: 1);
// Use the produce method
final foo2 = foo1.produce((draft) {
draft.value += 1;
});
assert(foo2.value == 2);
// Or create drafts inline
final fooDraft = foo.draft();
fooDraft.value = 10;
final foo3 = fooDraft.save();
assert(foo3.value == 10);
}
```
See the [examples](https://github.com/josiahsrc/draft) directory for more info.
## Set up
First install draft and build runner, if you haven't already.
```sh
dart pub add dev:build_runner dev:draft_builder draft
```
Next define your drafts.
```dart
// example.dart
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.
```sh
dart run build_runner build --delete-conflicting-outputs
```