From 0a47b1ef20e452e8cb1a1009bbcf83531ba4f9b7 Mon Sep 17 00:00:00 2001 From: Alexander Kirsch Date: Fri, 5 Aug 2022 22:10:12 +0200 Subject: [PATCH] improving documentation (#62) --- copy_with_extension_gen/README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/copy_with_extension_gen/README.md b/copy_with_extension_gen/README.md index 7139fb2..dd8baea 100644 --- a/copy_with_extension_gen/README.md +++ b/copy_with_extension_gen/README.md @@ -79,7 +79,7 @@ final copiedTwo = result.copyWith(id: "foo", text: null) // Results in BasicClas You can modify multiple fields at once using `copyWith` as a function like so: `myInstance.copyWith(fieldName: "test", anotherField: "test")`. Passing the `null` value to `non-nullable` fields will be ignored. -#### Nullifying instance fields: +#### Nullifying instance fields In order to nullify the class fields, an additional `copyWithNull` function can be generated. To make use of it, pass an additional parameter to your class annotation `@CopyWith(generateCopyWithNull: true)`. @@ -94,6 +94,35 @@ final int myImmutableField; By adding this annotation you forcing your generated `copyWith` to always copy this field as it is, without exposing it in the function interface. +#### Custom constructor name + +Set `constructor` if you want to use a named constructor, e.g. a private one. The generated fields will be derived from this constructor. + +```dart +@CopyWith(constructor: "_") +class SimpleObjectPrivateConstructor { + @CopyWithField(immutable: true) + final String? id; + final int? intValue; + + const SimpleObjectPrivateConstructor._({this.id, this.intValue}); +} +``` + +#### Skipping generation of `copyWith` functionality for individual fields + +Set `skipFields` to prevent the library from generating `copyWith` functions for individual fields e.g. `instance.copyWith.id("123")`. If you want to use only `copyWith(...)` function. + +```dart +@CopyWith(skipFields: true) +class SimpleObject { + final String id; + final int? intValue; + + const SimpleObject({required this.id, this.intValue}); +} +``` + ## How this library is better than `freezed`? It isn't. This library is a non-intrusive alternative for those who only need the `copyWith` functionality and do not want to maintain the codebase in the way implied by the framework. This library only requires from your side the annotation of your class with `CopyWith()` and an indication of the `.part` file, everything else is up to you. [`freezed`](https://pub.dev/packages/freezed), on the other hand, offers many more code generation features but requires your models to be written in a framework-specific manner.