Skip to content

Commit 90aa0eb

Browse files
authored
docs: add support for multiple constructors (#332)
1 parent 3bec349 commit 90aa0eb

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

packages/dart_mappable/doc/polymorphism.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ void main() {
127127
}
128128
```
129129

130+
## Multiple Constructors
131+
132+
You can also use factory constructors on the base class to provide a more convenient API for creating instances.
133+
134+
```dart
135+
// Set the discriminator key to "type".
136+
@MappableClass(discriminatorKey: 'type')
137+
abstract class Animal with AnimalMappable {
138+
const Animal(this.name);
139+
140+
// Factory constructors pointing to subclasses
141+
const factory Animal.nullAnimal(String name) = NullAnimal;
142+
143+
const factory Animal.defaultAnimal(String name, String type) = DefaultAnimal;
144+
145+
final String name;
146+
}
147+
```
148+
130149
## Custom Discriminator Logic
131150

132151
For a more advanced use-case where the discriminator key/value system is not enough, you can also

0 commit comments

Comments
 (0)