startTransaction method Null safety

StreamQueueTransaction<T> startTransaction()

Requests a transaction that can conditionally consume events.

The transaction can create copies of this queue at the current position using StreamQueueTransaction.newQueue. Each of these queues is independent of one another and of the parent queue. The transaction finishes when one of two methods is called:

Until the transaction finishes, this queue won't emit any events.

See also withTransaction and cancelable.

/// Consumes all empty lines from the beginning of [lines].
Future<void> consumeEmptyLines(StreamQueue<String> lines) async {
  while (await lines.hasNext) {
    var transaction = lines.startTransaction();
    var queue = transaction.newQueue();
    if ((await queue.next).isNotEmpty) {
      transaction.reject();
      return;
    } else {
      transaction.commit(queue);
    }
  }
}

Implementation

StreamQueueTransaction<T> startTransaction() {
  _checkNotClosed();

  var request = _TransactionRequest(this);
  _addRequest(request);
  return request.transaction;
}