2

I'm stuck with this custom query with variable using moor. No list is returned when using SELECT * FROM books WHERE title LIKE searchString;. Am I missing something?

code:

Stream<List<Book>> getFilteredBook(String searchString) {
    searchString = 'the';
    return customSelect(
      //query works fine
      //'SELECT * FROM books;',
      'SELECT * FROM books WHERE title LIKE searchString;',
      variables: [
        Variable.withString(searchString),
      ],
      readsFrom: {books},
    ).watch().map((rows) {
      // Turning the data of a row into a Book object
      return rows.map((row) => Book.fromData(row.data)).toList();
    });
  }
Tuss
  • 935
  • 1
  • 8
  • 14

1 Answers1

3

You can use a question mark (?) as a placeholder for your variable:

  Stream<List<Book>> getFilteredBook(String searchString) {
    searchString = 'the';
    return customSelect(
      //query works fine
      //'SELECT * FROM books;',
      'SELECT * FROM books WHERE title LIKE ?;',
      variables: [
        Variable.withString(searchString),
      ],
      readsFrom: {books},
    ).watch().map((rows) {
      // Turning the data of a row into a Book object
      return rows.map((row) => Book.fromData(row.data)).toList();
    });
  }
Hossein Yousefi
  • 889
  • 8
  • 18
  • you're welcome @Tuss, feel free to mark my answer as accepted. – Hossein Yousefi Nov 30 '21 at 21:58
  • @HosseinYousefi I did same with my table but the result is null and written a simple select query without any variables. Can you pls help to for that? Do I need to follow any steps before coming to this method? – Chirag Prajapati Jul 21 '22 at 07:22