0

My question will seem quite simple, but I'm having a hard time understanding how certain queries work with ObjectBox and Flutter.

I have a List object. I would like to retrieve them all, but display only the first 5, sorted by ascending name.

I find many examples with filter queries in the documentation, but the getAll() does not allow to easily integrate the limit and I find very few examples. I think I'm doing it wrong. What is the best way to do this query ?

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();
Sheitak
  • 71
  • 9

1 Answers1

0

I see that you want to get all your results but you want to display the first 5. So what I would do in your case is:

(1) get all your results(based on your query) (2) then set another property equal to the top 5:)

** now you have a list of all your entries and you have your top 5 that you can display.

heres how I would code it up!

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;
}

Hope this helps:)

Happy Coding, also I'm a developer @Pieces and if you need a place to store helpful code snippets, like this one, check it out here https://code.pieces.app/

Mark Widman
  • 131
  • 5
  • Hello @Mark Widman and thank you for your intervention. Your idea is to be explored, I will dig on this side. Thanks ! In fact, I was counting on being able to work on my request so that it would be sorted and limited immediately. Other than the first 5, I would like them to be sorted in ascending name... Might be complicated to do that way. I will study. Also, I probably have to use getAll() because boxListCoins.query()..order() only returns me the 1st result in a loop. But I can follow your operation and I write here if I have news. Thanks for your resource, I'll take a look – Sheitak Jul 18 '22 at 13:57
  • lemme know if I can help out in anyway! and feel free to give an upvote if you thought my response was helpful <3 – Mark Widman Jul 18 '22 at 19:45
  • Just some tips: a query with no condition is equal to a getAll. You can `..limit = 5` the number of query results returned. https://docs.objectbox.io/queries#limit-offset-and-pagination Ordering only some of the results is not possible, you'd have to write your own code for that. https://docs.objectbox.io/queries#ordering-results – Uwe - ObjectBox Jul 19 '22 at 05:28