Transactions¶
Transactions are useful when you want to perform multiple operations. With Flat you have two options to start a transaction:
-
Inside a DAO:
You can annotate a method with
@transaction
and make that methodasync
. Also you have toawait
for every database call you make inside that method.@transaction Future<void> replacePersons(List<Person> persons) async { await deleteAllPersons(); await insertPersons(persons); }
-
On database:
You can call
transaction
on your database with a callback which will get called whenever transaction created with a database object for you to do your operations on. Again it's required toawait
for every database call.await database.transaction<void>((dynamic db) async { if (db is AppDatabase) { await db.personDao.deleteAllPersons(); await db.personDao.insertPersons(persons); } });
Attention
- Transactions can return objects