batch method Null safety

Future<void> batch(
  1. _BatchRunner runInBatch

Runs statements inside a batch.

A batch can only run a subset of statements, and those statements must be called on the Batch instance. The statements aren't executed with a call to Batch. Instead, all generated queries are queued up and are then run and executed atomically in a transaction. If batch is called outside of a transaction call, it will implicitly start a transaction. Otherwise, the batch will re-use the transaction, and will have an effect when the transaction completes. Typically, running bulk updates (so a lot of similar statements) over a Batch is much faster than running them via the GeneratedDatabase directly.

An example that inserts users in a batch:

 await batch((b) {
       TodosCompanion.insert(content: 'Use batches'),
       TodosCompanion.insert(content: 'Have fun'),


Future<void> batch(_BatchRunner runInBatch) {
  final engine = resolvedEngine;

  final batch = Batch._(engine, engine is! Transaction);
  final result = runInBatch(batch);

  if (result is Future) {
    return result.then((_) => batch._commit());
  } else {
    return batch._commit();