【问题标题】:Adding distinct to a flutter moor query向颤动的沼泽查询添加不同的
【发布时间】:2020-06-04 05:53:15
【问题描述】:

我有以下颤振沼泽查询

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

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

更新: 我要写的查询是:

select DISTINCT garnishName from RecipeGarnishes where postGarnish = 'true'

garnishNamepostGarnish 是我的 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]

[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] 未处理的异常:NoSuchMethodError:getter 'garnishName' 被调用为 null。

【问题讨论】:

标签: flutter flutter-moor


【解决方案1】:

查询本身是正确的,但您无法读取这样的结果: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();

【讨论】:

  • 知道了,谢谢。喜欢这个包,期待将来在我的项目中使用它:P :D
【解决方案2】:

您可以按如下方式添加 distinct

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

【讨论】:

  • 我需要在 garnishName 列中添加不同的条件,而不是整个行。
  • 对于仅获取特定列,您可以使用 selecyOnly 代替 select,并使用 addColumns 方法。否则,您也可以使用 customSelect 输入您自己的查询。
  • 嗨,我用 selectOnly 更新了我的问题。仍然收到错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
相关资源
最近更新 更多