insert method

Future<int> insert(
  1. ISQLiteItem item
)

Implementation

Future<int> insert(ISQLiteItem item) async {
  final db = await getOpenDatabase();
  final map = item.toMap();
  final tableName = item.getTableName();
  final primaryKey = item.getPrimaryKeyName();

  // Remove any fields not existing in the current table schema
  final existingColumns = await tableColumns(tableName, db: db);
  map.removeWhere((key, _) => !existingColumns.contains(key));

  // If primary key is explicitly 0 or null, remove it so SQLite will autogenerate
  if (map.containsKey(primaryKey)) {
    final value = map[primaryKey];
    if (value == null || value == 0) {
      //map.remove(primaryKey);
      map[primaryKey] = null;
    }
  }

  final rowId = await db.insert(tableName, map);
  return rowId;
}