-
-
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
1 parent
64d0732
commit 4676207
Showing
6 changed files
with
166 additions
and
56 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,64 @@ | ||
// ignore_for_file: avoid_unused_constructor_parameters | ||
|
||
import 'package:freezed/freezed.dart'; | ||
|
||
// The usual syntax, based on constructors. | ||
// We no-longer use "factory" constructors. | ||
@Freezed() | ||
class Person { | ||
Person( | ||
String name, { | ||
required int age, | ||
}); | ||
} | ||
|
||
// Alternatively, you can define classes using fields: | ||
|
||
@Freezed() | ||
class Person2 { | ||
final String name; | ||
final int age; | ||
} | ||
|
||
void main() { | ||
var person = Person('John', age: 30); | ||
person = person.copyWith(age: 31); | ||
|
||
print(person.name); // John | ||
print(person.age); // 31 | ||
|
||
// Person2 works the same! | ||
var person2 = Person2(name: 'John', age: 30); | ||
person2 = person2.copyWith(age: 31); | ||
} | ||
|
||
// ---- Advanced usage: Unions ---- | ||
|
||
class Engine {} | ||
|
||
@Freezed() | ||
sealed class Vehicle { | ||
final Engine engine; | ||
} | ||
|
||
@Freezed() | ||
class Plane implements Vehicle { | ||
final Wing wing; | ||
|
||
// Currently necessary due to augmentation libraries not being fully implemented. | ||
// This won't be needed in the future. | ||
@override | ||
final Engine engine; | ||
} | ||
|
||
class Wing {} | ||
|
||
@Freezed() | ||
class Car implements Vehicle { | ||
final Wheel wheel; | ||
|
||
@override | ||
final Engine engine; | ||
} | ||
|
||
class Wheel {} |
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,45 @@ | ||
import 'package:freezed/freezed.dart'; | ||
|
||
@Freezed() | ||
class Simple { | ||
final int foo; | ||
final String bar; | ||
final double initialized = 42; | ||
} | ||
|
||
@Freezed(constructor: 'custom') | ||
class Named { | ||
final int foo; | ||
} | ||
|
||
class SuperBase { | ||
SuperBase.custom(int value) : value = value * 2; | ||
final int value; | ||
} | ||
|
||
@Freezed(superCtor: 'custom') | ||
class Super extends SuperBase { | ||
final int foo; | ||
} | ||
|
||
// TODO uncomment once "super.field" works in augmentation constructors. | ||
// @Freezed() | ||
// sealed class ExtendsBase { | ||
// final int a; | ||
// } | ||
|
||
// @Freezed() | ||
// class Extends extends ExtendsBase { | ||
// final int b; | ||
// } | ||
|
||
// TODO augmentations are confused once again | ||
// @Freezed() | ||
// sealed class ImplementsBase { | ||
// final int a; | ||
// } | ||
|
||
// @Freezed() | ||
// class Implements implements ImplementsBase { | ||
// final int b; | ||
// } |
This file was deleted.
Oops, something went wrong.
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