search method

Future<List<ISQLiteItem>> search(
  1. ISQLiteItem item,
  2. String columnName,
  3. String query, {
  4. int? limit,
})

Performs a case-insensitive search in a specified column of an SQLite table.

This method searches for the specified query in the columnName of the tableName and returns a list of items that match the search criteria. It uses the LIKE operator for searching and COLLATE NOCASE for case-insensitivity.

Parameters:

  • item: An instance of ISQLiteItem representing the database table schema.
  • columnName: The name of the column to search in.
  • query: The search query to match against the specified column.
  • limit: (Optional) The maximum number of results to return.

Returns a list of ISQLiteItem objects that match the search criteria.

Example:

List<ISQLiteItem> searchResults = await search(
  MyDatabaseItem(), // Replace with your ISQLiteItem implementation
  'title',
  'flutter',
  limit: 10,
);

for (var result in searchResults) {
  print(result.toString());
}

Implementation

Future<List<ISQLiteItem>> search(
    ISQLiteItem item, String columnName, String query,
    {int? limit}) async {
  var database = await getOpenDatabase();
  String table = item.getTableName();
  List<ISQLiteItem> results = [];
  var maps = await database.query(
    table,
    columns: null, // Fetch all columns
    where: "$columnName LIKE ? COLLATE NOCASE",
    whereArgs: ['%$query%'],
    limit: limit,
  );
  results = maps.map((map) => item.fromMap(map)).toList();
  return results;
}