向 flutter moor 查询添加 distinct

Adding distinct to a flutter moor query

我有以下 flutter moor 查询

(select(recipeGarnishes)..where((tbl) => tbl.postGarnish.equals(true))).get();

如何将 distinct 条件添加到查询中?

更新: 我要写的查询是:

select DISTINCT garnishName from RecipeGarnishes where postGarnish = 'true'

garnishNamepostGarnish 是我的 RecipeGarnishes table

中的列

更新 2:

根据答案,我尝试了这个。

final query = selectOnly(recipeGarnishes, distinct: true)
                ..addColumns([recipeGarnishes.garnishName])
                ..where(recipeGarnishes.postGarnish.equals(postGarnish));
List<TypedResult> result = await query.get();

return result.map((row) => row.readTable(recipeGarnishes)).toList();

但是它给我以下错误

Moor: Sent SELECT DISTINCT recipe_garnishes.garnish_name AS "recipe_garnishes.garnish_name" FROM recipe_garnishes WHERE recipe_garnishes.post_garnish = ?; with args [1]

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'garnishName' was called on null.

您可以按如下方式添加distinct

(select(recipeGarnishes, distinct: true)..where((tbl) => tbl.postGarnish.equals(true))).get();

查询本身是正确的,但您不能这样读取结果:row.readTable 会 return 包含所有列的整行。但是,您只有 select 单列 (garnishName),因此 moor 无法加载该行,而是 returns null

您可以使用

final query = selectOnly(recipeGarnishes, distinct: true)
                ..addColumns([recipeGarnishes.garnishName])
                ..where(recipeGarnishes.postGarnish.equals(postGarnish));

return query.map((row) => row.read(recipeGarnishes.garnishName)).get();