insertObjetList method
Inserts a list of entity objects into their corresponding table.
- All objects must be instances of the same entity class.
- Returns
trueif every row was inserted successfully,falseotherwise (null/empty list, missing column, constraint violation, ...).
Implementation
Future<bool> insertObjetList(List<Object?>? objectlist) async {
if (objectlist == null || objectlist.isEmpty) return false;
final Object? first = objectlist.first;
if (first == null) return false;
final String table = first.runtimeType.toString();
await createTableIfNotExists(first);
bool allInserted = true;
await (await db).transaction((txn) async {
for (final Object? object in objectlist) {
if (object == null) continue;
try {
final int rowId = await txn.insert(
table,
mapToUse((object as dynamic).toMap() as Map<String, dynamic>),
);
if (rowId <= 0) {
allInserted = false;
break;
}
} catch (e, s) {
debugPrint('insertObjetList failed: $e\n$s');
allInserted = false;
break;
}
}
});
return allInserted;
}