简体   繁体   中英

How to write customSelect query using Flutter moor/drift

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();
    });
  }

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();
    });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM