write method Null safety

Future<int> write(
  1. Insertable<D> entity,
  2. {bool dontExecute = false}
)

Writes all non-null fields from entity into the columns of all rows that match the where clause. Warning: That also means that, when you're not setting a where clause explicitly, this method will update all rows in the table.

The fields that are null on the entity object will not be changed by this operation, they will be ignored.

When dontExecute is true (defaults to false), the query will NOT be run, but all the validations are still in place. This is mainly used internally by drift.

Returns the amount of rows that have been affected by this operation.

See also: replace, which does not require where statements and supports setting fields back to null.

Implementation

Future<int> write(Insertable<D> entity, {bool dontExecute = false}) async {
  _sourceTable.validateIntegrity(entity).throwIfInvalid(entity);

  _updatedFields = entity.toColumns(true)
    ..remove((_, value) => value == null);

  if (_updatedFields.isEmpty) {
    // nothing to update, we're done
    return Future.value(0);
  }

  if (dontExecute) return -1;
  return await _performQuery();
}