deleteSingle method

Future<int> deleteSingle({
  1. @required required dynamic objectToDelete,
})

This method is used to delete a single object from the database.

It takes an object as a parameter and deletes the corresponding record from the database. If the delete operation is successful, the method returns the number of rows affected. If an error occurs during the delete operation, the method logs the error and returns -1.

The method first gets the database instance. Then, it uses reflection to get the primary key(s) of the object. It constructs a WHERE clause for the delete operation using the primary key(s) and their values.

Then, it deletes the corresponding record from the database. The table name for the delete operation is obtained by calling the getTableName method with the object.

Parameters: objectToDelete (dynamic): The object to delete from the database.

Returns: Future

Implementation

Future<int> deleteSingle({@required required dynamic objectToDelete}) async {
  var db = await getDatabase();

  InstanceMirror reflectNew = reflector.reflect(objectToDelete);
  List<String> primary =
      reflectNew.type.invokeGetter("primary") as List<String>;
  List<dynamic> whereArgs = [];

  /* Construct the WHERE clause. */
  String where = '';
  // Primary keys
  for (var x in primary) {
    whereArgs.add(reflectNew.invokeGetter(x));

    where = makeWhere(where, x, primary);
  }

  try {
    var result = await db!.delete(getTableName(objectToDelete),
        where: where, whereArgs: whereArgs);
    return result;
  } catch (e) {
    PrintHandler.warningLogger.e(
        '⛔sqflite_simple_dao_backend⛔: Error deleting record: $e. "-1" returned.');
    return -1;
  }
}