Explain @required in data classes (#548)

This commit is contained in:
Simon Binder 2020-07-13 15:14:45 +02:00
parent 74caeced85
commit e0ce093361
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 5 additions and 1 deletions

View File

@ -69,13 +69,17 @@ be used when inserting a new row. To set a constant default value, use `withDefa
```dart ```dart
class Preferences extends Table { class Preferences extends Table {
TextColumn get name => integer().autoIncrement()(); TextColumn get name => text()();
BoolColumn get enabled => boolean().withDefault(const Constant(false))(); BoolColumn get enabled => boolean().withDefault(const Constant(false))();
} }
``` ```
When you later use `into(preferences).insert(PreferencesCompanion.forInsert(name: 'foo'));`, the new When you later use `into(preferences).insert(PreferencesCompanion.forInsert(name: 'foo'));`, the new
row will have its `enabled` column set to false (and not to null, as it normally would). row will have its `enabled` column set to false (and not to null, as it normally would).
Note that columns with a default value (either through `autoIncrement` or by using a default), are
still marked as `@required` in generated data classes. This is because they are meant to represent a
full row, and every row will have those values. Use companions when representing partial rows, like
for inserts or updates.
Of course, constants can only be used for static values. But what if you want to generate a dynamic Of course, constants can only be used for static values. But what if you want to generate a dynamic
default value for each column? For that, you can use `clientDefault`. It takes a function returning default value for each column? For that, you can use `clientDefault`. It takes a function returning