insertObjetList method

Future<bool> insertObjetList(
  1. List<Object?>? objectlist, {
  2. bool orReplace = false,
})

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, ...).
  • As in 2.0.3, rows inserted before a failing row are kept; call it inside transaction for all-or-nothing.
  • With orReplace, rows having the same key are replaced.

Implementation

Future<bool> insertObjetList(
  List<Object?>? objectlist, {
  bool orReplace = false,
}) async {
  if (objectlist == null || objectlist.isEmpty) return false;
  final Object? first = objectlist.first;
  if (first == null) return false;

  final String table = _tableOf(first);
  await createTableIfNotExists(first);

  final SqlExecutor executor = await _executor();
  for (int attempt = 0; ; attempt++) {
    try {
      return await executor.transaction((SqlExecutor tx) async {
        for (final Object? object in objectlist) {
          if (object == null) continue;
          try {
            final int rowId = await _insertRow(
              tx,
              table,
              object,
              orReplace: orReplace,
            );
            if (rowId <= 0) return false;
          } catch (e, s) {
            if (strict ||
                _scope != null ||
                (e is DatabaseException && e.isNoSuchTableError())) {
              rethrow;
            }
            _report('insertObjetList', e, s);
            return false;
          }
        }
        return true;
      });
    } on DatabaseException catch (e, s) {
      if (attempt == 0 && e.isNoSuchTableError()) {
        _knownTables.remove(table);
        await createTableIfNotExists(first);
        continue;
      }
      if (strict || _scope != null) rethrow;
      _report('insertObjetList', e, s);
      return false;
    }
  }
}