我的问题看起来很简单,但我很难理解某些查询是如何与ObjectBox和Flutter一起工作的。
我有一个列表对象。我想检索它们,all,但只显示前5,按升序名称排序。
我在文档中找到了许多带有过滤器查询的示例,但是getAll()不允许很容易地集成这个限制,而且我发现很少有例子。我想我做错了。执行此查询的最佳方法是什么?
final boxListCoins = objectBoxDatabase.store.box<CoinsList>();
// final query = boxListCoins.getAll(); ?
final query = (boxListCoins.query()..order(CoinsList_.coinId, flags: Order.descending)).build();
query.limit = 5;
final coinsList = query.find();
发布于 2022-07-18 13:00:13
我看到你想要得到你所有的结果,但是你想要显示前5。所以在你的情况下,我要做的是:
(1)获取所有结果(基于查询) (2),然后设置另一个属性,等于前5:)
**现在你有了你所有条目的列表,你有了你可以显示的前5位。
这是我如何编码它!
final boxListCoins = objectBoxDatabase.store.box<CoinsList>();
// final query = boxListCoins.getAll(); ?
final query = (boxListCoins.query()..order(CoinsList_.coinId, flags: Order.descending)).build();
// this is your list of all of the coins!
final coinsList = query.find();
// This is you list of the Top 5
var topFive = [];
if (coinsList.length >= 5) {
// take your top5 here :) also feel free to turn this iterable into what ever you want I just choose to keep it a list :)
topFive = coinsList.take(5).toList();
} else {
// if you length is not greater than 5 then you have your top automatically.
topFive = coinsList;
}
希望这会有所帮助:)
愉快的编码,我也是一个开发人员@片段,如果您需要一个地方存储有用的代码片段,比如这个,请在这里查看https://code.pieces.app/
https://stackoverflow.com/questions/73022520
复制相似问题