You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中使用Moor查询以指定字符开头的表行数据

问题原因

你当前的查询语句是精确匹配Name字段值完全等于R2的条目,没有匹配前缀所需的SQL通配符,因此无法筛选出以R2开头的其余条目。

正确实现方式

方式1:使用like语法手动加通配符

SQL的like查询中,%代表匹配任意长度的任意字符,你只需要在匹配前缀后加%即可:

Future<List<Item>> getFilteredItems() => (select(items)..where((t) => t.Name.like('R2%'))).get();

方式2:使用内置startsWith方法(更推荐)

Drift(原Moor)库已经封装了前缀匹配的专用方法,不需要手动拼接通配符,可读性更高:

Future<List<Item>> getFilteredItems() => (select(items)..where((t) => t.Name.startsWith('R2'))).get();
补充说明

如果需要大小写不敏感的前缀匹配,可以使用小写匹配的变种方法:

// 忽略大小写匹配以R2开头的条目,r2、R2、R2xxx等格式都能命中
Future<List<Item>> getFilteredItems() => (select(items)..where((t) => t.Name.lowerStartsWith('r2'))).get();

内容的提问来源于stack exchange,提问作者West

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 17:54:02