-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Migrate from v2 to v3 | ||
|
||
### Require keyword (`sealed` / `abstract`) | ||
|
||
Classes using the factory constructor now require a keyword `sealed` / `abstract`. | ||
|
||
```diff | ||
@freezed | ||
-class Person with _$Person { | ||
+abstract class Person with _$Person { | ||
const factory Person({ | ||
required String firstName, | ||
required String lastName, | ||
required int age, | ||
}) = _Person; | ||
|
||
factory Person.fromJson(Map<String, Object?> json) | ||
=> _$PersonFromJson(json); | ||
} | ||
``` | ||
|
||
```diff | ||
@freezed | ||
-class Model with _$Model { | ||
+sealed class Model with _$Model { | ||
factory Model.first(String a) = First; | ||
factory Model.second(int b, bool c) = Second; | ||
} | ||
``` | ||
|
||
### Pattern matching | ||
|
||
Freezed no longer generates `.map`/`.when` extensions and their derivatives for freezed classes used for pattern matching. Instead, use Dart's built-in [pattern matching](https://dart.dev/language/patterns#matching) syntax. | ||
|
||
```diff | ||
final model = Model.first('42'); | ||
|
||
-final res = model.map( | ||
- first: (String a) => 'first $a', | ||
- second: (int b, bool c) => 'second $b $c', | ||
-); | ||
+final res = switch (model) { | ||
+ First(:final a) => 'first $a', | ||
+ Second(:final b, :final c) => 'second $b $c', | ||
+}; | ||
|
||
``` |