Transaction class final
A transaction proxy object for executing writes and reads atomically.
Obtained via Database.transaction. All operations use the writer connection, so reads see uncommitted writes from earlier statements in the same transaction.
Supports nested transactions via transaction, which uses SQLite SAVEPOINTs under the hood:
await db.transaction((tx) async {
await tx.execute('INSERT INTO users(name) VALUES (?)', ['Ada']);
// Nested transaction — uses SAVEPOINT internally.
await tx.transaction((inner) async {
await inner.execute('INSERT INTO users(name) VALUES (?)', ['Bob']);
// Throw here to roll back only Bob's insert.
});
final rows = await tx.select('SELECT COUNT(*) as c FROM users');
print(rows.first['c']); // includes Ada (and Bob if inner didn't throw)
});
Constructors
- Transaction(Writer _writer)
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → void -
execute(
String sql, [List< Object?> parameters = const []]) → Future<WriteResult> - Executes a write statement within this transaction.
-
executeBatch(
String sql, List< List< paramSets) → Future<Object?> >void> - Executes one SQL statement across many parameter sets within this transaction.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
select(
String sql, [List< Object?> parameters = const []]) → Future<List< Map< >String, Object?> > - Executes a query within this transaction, seeing uncommitted writes.
-
toString(
) → String -
A string representation of this object.
inherited
-
transaction<
T> (Future< T> body(Transaction tx)) → Future<T> -
Initiates a nested transaction as a new savepoint. If
bodycompletes normally, the savepoint is released (changes become part of the enclosing transaction). Ifbodythrows, the savepoint is rolled back (only this nested transaction's changes are undone) and the exception is rethrown.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- current → Transaction?
-
Returns the current Transaction if any.
no setter
Constants
- currentZoneKey → const Symbol
- Zone key storing the active Transaction when inside a transaction body. Database methods check this to transparently route through the transaction instead of deadlocking on the write lock.