Flutter Isar Database — part 4 — Filter data

Flutter Isar Database — part 4 — Filter data

In the last article, we dealt with how to create, read, update and delete records. Today we will look at how we filter the data that we are querying.

If you are new to this series, check out this list.

Happy reading!

Basics

There are two different ways how to filter data. You can do it with filters and with where clauses. We will look at both methods.
But, what is a filter exactly? Let’s assume you want to get all the users that start with “A”. That is a filter. You only get data that meets the criteria. Okay, after we’ve learned that, we will take a look at filters.

Filters

In the last article, we’ve already seen how you can query for specific data. It works the following:

But as you surely can think of, there are many more filters. Here is an example of all of them:

But what if you want to combine different filters? Let’s say, I want a filter that returns me the values greater and equal to ten:

There are some other logical operators that I will list here:

  • .and() - Evaluates trueif both left-hand and right-hand expressions are true.
  • .or() - Evaluates to true if either expression returns true.
  • not() - Negates the result of the following expression.
  • .group()- Group conditions and allow to specify the order of evaluation.

It’s also possible to only query something if something other results in true (optional querying). Let’s say you get an integer that can be null. You first have to be sure, that this integer is not null and then query for this integer.

There is also a query modifier called repeat(values, queryBuilder), which extends the query for each value in values:

Where clause

Okay, let’s talk about the other option you have. Where clause. They are a very powerful tool, but it can be quite difficult to get them right. But what’s the difference now to filters? Very simple. Where clauses query the database using an index, which is way faster than filtering each record individually. If you know a little bit about databases, you should already know what an index is. If not, you can read a little bit about it here. The documentation itself says:

“As a basic rule, you should always try to reduce the records as much as possible using where clauses and do the remaining filtering using filters” (Source: https://isar.dev/queries.html#where-clauses)

Okay, to use where clauses, we first have to place an index on different parameters. We will use the example provided in the documentation for that:

Now we have placed one index on size and the other index on isUnisex.

Now we can write where clauses, for example sizeEqualTo() or isUnisexEqualTo(). Okay, I think everything understood this. But now the difficult stuff happens. We can also write isUnisexSizeEqualTo(). But what does that do? Well, after explaining it, it will be very simple: We combined the previous two functions into one. That also means we have to provide two parameters to that function:

Great, we now know also know how to use where clauses. Let’s see how we can combine where clauses and filters.

Combining where clause and filters

It’s very easy to combine where clauses and filters. First, we provide the where clause, and before we write findAll(); we just add the filter. Let’s look at an example of that:

Further reading & Conclusion

In this article, you have enhanced your skills in the local database solution “Isar”. You have seen how easy it is to use and how you can write great queries.

You can unfold the whole power of Isar if you use packages like Freezed, Formz or Flutter Hooks. If you want to learn these additions, I have whole tutorials about them. Check them out here.

In the next few articles, I will introduce more somewhat complicated packages and will continue this series. If you don’t want to miss this, I recommend you to follow me.

I tried my best to write the easiest tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

NOTICE: This article is based on the official Isar documentation. All the information provided here is from this site and a big part of the source code is taken from the documentation. Isar Documentation Source: https://isar.dev/.

Did you find this article valuable?

Support Tomic Riedel by becoming a sponsor. Any amount is appreciated!