Basic Queries
One of the essential features in many applications is finding specific data in a fast and straightforward way. Parse has excellent tools for data querying, such as comparing and ordering results, while being able to chain and combine queries into more complex ones. In this guide, you will perform basic queries in Parse and implement Flutter widgets using these queries. You will learn how to set up and query realistic data using your Back4App and Flutter.
To complete this tutorial, you will need:
- An app created on Back4App.
- An Flutter app connected to Back4app.
- Note: Follow the Install Parse SDK on Flutter project to create an Flutter Project connected to Back4App.
- A device (or virtual device) running Android or iOS.
Show how basic data queries work and how to perform them in Parse.
Any Parse query operation uses the QueryBuilder object type, which will help you retrieve specific data from your database throughout your app. It is crucial to know that a QueryBuilder will only resolve after calling a retrieve method query, so a query can be set up and several modifiers can be chained before actually being called.
To create a new QueryBuilder, you need to pass as a parameter the desired QueryBuilder subclass, which is the one that will contain your query results.
An example query can be seen below, in which a fictional Profile subclass is being queried.
You can read more about the QueryBuilder class here at the official documentation.
Before going into this step, let’s create our example Profile class, which will be the target of our queries in this guide.
Go to the main.dart file, clean up all the code, and replace it with:
Find your Application Id and Client Key credentials navigating to your app Dashboard at Back4App Website.
Update your code in main.dart with the values of your project’s ApplicationId and ClientKey in Back4app.
- keyApplicationId = App Id
- keyClientKey = Client Key
Run the project, and the app will create Profile class.
Note: When debug parameter in function Parse().initialize is true, allows displaying Parse API calls on the console. This configuration can assist in debugging the code. It is advisable to disable debug in the release version.
Now that you have a populated class, we can now perform some basic queries in it.
Let’s begin by filtering Profile results by name, which is a string type field, searching for values that contain the name Adam using the QueryBuilder.whereContains method:
Let’s now query by the number type field friendCount by using another common query method, QueryBuilder.whereGreaterThan. In this case, we want user Profiles in which the friend count is greater than 20.
Other recurring query methods are QueryBuilder.orderByAscending and QueryBuilder.orderByDescending, responsible for ordering your queries. This ordering can be done in most data types, so let’s order a query by the date field birthDay by the youngest.
As stated here before, you can and should chain query methods to achieve more refined results. Let’s then combine the previous examples in a single query request:
Let’s now use our example queries inside a app in Flutter, with a simple interface having a list showing results and also 4 buttons for calling the queries. This is how the component code is laid out, note the doQuery functions, containing the example code form before.
Go to the main.dart file, clean up all the code, and replace it with:
Find your Application Id and Client Key credentials navigating to your app Dashboard at Back4App Website.
Update your code in main.dart with the values of your project’s ApplicationId and ClientKey in Back4app.
- keyApplicationId = App Id
- keyClientKey = Client Key
Run the project.
This is how the app should look like after rendering and querying by all the query functions:
At the end of this guide, you learned how basic data queries work and how to perform them in Parse on Flutter.
In the next guide, you will learn about all of querying possibilities and methods in Parse.