Moor: How to select only few columns(more than two and specific) with distinct values? using Stream

Created on 27 Feb 2020  路  6Comments  路  Source: simolus3/moor

I'm trying in this manner
`Stream getFilteringValues() {
final itemType = items.itemType;
final query = selectOnly(items)..addColumns([itemType]);

return query.map((row) => row.readTable(items)).watch();

}`

question

All 6 comments

You can use selectOnly(items, distinct: true), is that what you need?

i want to use SELECT itemType, itemCategory from items WHICH IS DISTINCT

I'm getting null while calling
Screenshot 2020-02-27 at 4 01 53 PM

I'm not sure I fully understand the question. If you want to run SELECT itemType, itemCategory from items, you can use selectOnly(items)..addColumns([items.itemType, items.itemCategory]).

The reason row.readTable(items) returns null is that moor can't know whether you're selecting all columns from the items table. For instance, let's say the items table had a non-nullable column foo, which you're not selecting. If we made row.readTable(items).foo return null because it's not in the query, we'd have to make it a nullable field after Dart gets non-nullable types. That would be annoying for users writing regular queries where we know foo isn't null.

For now you could probably do

Stream getFilteringValues() {
  final itemType = items.itemType;
  final query = selectOnly(items)..addColumns([itemType]);
  return query.map((row) => Item(itemType: row.read(itemType)).watch();
}

But bear in mind that this is likely to break with NNBD.

I'm still getting issues while using this
can you give me a simple solution to select few columns from a table where i can get their distinct values.

Suppose In

Items Table i have itemType Column where values would be like ["EAR","BRC","PEN","EAR","BRC"]
so from this i want only ["EAR","BRC","PEN"]

And in Second Column itemCategory i would have ["Gold","Plain","Gold"]
i want only ["Gold","Plain"]

Screenshot 2020-02-27 at 4 08 43 PM

I would write two queries for this:

Items Table i have itemType Column where values would be like ["EAR","BRC","PEN","EAR","BRC"] so from this i want only ["EAR","BRC","PEN"]

Stream<List<String>> filterItemTypes() {
  final itemType = items.itemType;
  final query = selectOnly(items, distinct: true)..addColumns([itemType]);
  return query.map((row) => row.read(itemType));
}

And in Second Column itemCategory i would have ["Gold","Plain","Gold"] i want only ["Gold","Plain"]

Stream<List<String>> filterCategories() {
  final itemCategory = items.itemCategory;
  final query = selectOnly(items, distinct: true)..addColumns([itemCategory]);
  return query.map((row) => row.read(itemCategory));
}
Was this page helpful?
0 / 5 - 0 ratings