0

TLDR: "SELECT DISTINCT category FROM items" efficiently in Drift

There is Item (or ItemsCompanion) data class that among others has 'String category' property. I'd like to get a list of distinct categories efficiently.

I'm failing to found a way

  1. to get in result property of object without object itself
  2. to filter out only unique values

Any help would be appreciated.

I can get a list of Items, iterate through and put it's categories in List, transform a List into a Set but it seems way too inefficient.

Ivan C
  • 116
  • 1
  • 4

1 Answers1

1

Using the answer from simolus3 https://stackoverflow.com/a/62359317/8108153 I got this:

 Future<List<String>> getItemsCategories() {
    final query = selectOnly(items, distinct: true)
          ..addColumns([items.category])
          ..where(items.category.isNotNull());
        ;

    return query.map((row) => row.read(items.category)).get();
  }

Whether it's the optimal approach or not yet it's way better anything I've come up with

Ivan C
  • 116
  • 1
  • 4