toListWhereMapMatch method
Future<List<ISQLiteItem> >
toListWhereMapMatch(
- ISQLiteItem item,
- Map<
String, dynamic> columnValues
Implementation
Future<List<ISQLiteItem>> toListWhereMapMatch(
ISQLiteItem item,
Map<String, dynamic> columnValues,
) async {
List<ISQLiteItem> results = [];
var db = await getOpenDatabase();
String tableName = item.getTableName();
// Check if the columnValues map is empty
if (columnValues.isEmpty) {
return [];
}
// Build the WHERE clause dynamically based on the provided column names
List<String> whereConditions = [];
List<dynamic> whereArgs = [];
columnValues.forEach((columnName, value) {
whereConditions.add('$columnName = ?');
whereArgs.add(value);
});
String condition = whereConditions.join(' AND ');
try {
// Query the database
var maps = await db.query(
tableName,
where: condition,
whereArgs: whereArgs,
);
// Convert the query results into a list of ISQLiteItem objects
results = maps.map((map) => item.fromMap(map)).toList();
} catch (e) {
// Handle any database errors that may occur
//print('Error querying database: $e');
}
return results;
}