2

我有以下颤动的沼泽查询

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

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

更新:我想写的查询是:

select DISTINCT garnishName from RecipeGarnishes where postGarnish = 'true'

garnishName并且postGarnish是我RecipeGarnishes表中的列

更新 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:发送 SELECT DISTINCT recipe_garnishes.garnish_name AS "recipe_garnishes.garnish_name" FROM recipe_garnishes WHERE recipe_garnishes.post_garnish = ?; 带参数 [1]

[错误:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'garnishName'。

4

2 回答 2

5

查询本身是正确的,但您无法读取这样的结果:row.readTable将返回包含所有列的完整行。但是,您只选择了单个列 ( garnishName),因此 moor 无法加载该行并返回null

您可以使用

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

return query.map((row) => row.read(recipeGarnishes.garnishName)).get();
于 2020-06-13T11:48:15.123 回答
1

您可以按如下方式添加不同的

(select(recipeGarnishes, distinct: true)..where((tbl) => tbl.postGarnish.equals(true))).get();
于 2020-06-09T05:06:25.783 回答