insertObjetList method

Future<bool> insertObjetList(
  1. List<Object?>? objectlist
)

Inserts a list of entity objects into their corresponding table.

  • All objects must be instances of the same entity class.
  • Returns true if every row was inserted successfully, false otherwise (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;
}