insertReturningOrNull method

Future<Row?> insertReturningOrNull(
  1. Insertable<Row> entity, {
  2. UpsertClause<Row, RS>? onConflict,
})

Inserts a row into the table and returns it.

Depending on how the statement is configured, it's possible that an insert does not actually insert a row (e.g. if a DoNothing clause has been added as an upsertClause). This method returns null in that case.

Implementation

Future<Row?> insertReturningOrNull(
  Insertable<Row> entity, {
  UpsertClause<Row, RS>? onConflict,
}) async {
  source = null;
  values(entity);
  if (onConflict != null) {
    this.onConflict(onConflict);
  }

  final rows = await runReturning();
  if (rows.isNotEmpty) {
    return rows.single;
  } else {
    return null;
  }
}