Flutter
...
Parse SDK (REST)
Data Queries

Relational Queries

11min

Parse Relational Query in Flutter

Introduction

You’ve already seen how to store relational data objects using theParse.Pointer and Parse.Relations data types. You’ve learned that on Parse it is possible to use any ParseObject as a value in other ParseObject, establishing a relation between them. Internally, the Parse framework will store the referred-to object in just one place to maintain consistency. That can give you extra power when building and running complex queries.

Also, you’ve already learned how to use a QueryBuilder with get can retrieve a single ParseObject from Back4App. There are many other ways to retrieve data with QueryBuilder.

In this guide, you will ding deep into the QueryBuilder class and see methods you can use to build Relational Queries. You will use a simple database class with some mocked data to perform the Queries using Flutter on Back4App.

Prerequisites

To complete this tutorial, you will need:

Goal

Query relational data stored on Back4App from a Flutter App.

1 - Understanding the QueryBuilder class

Any Parse query operation uses the QueryBuilder object type, which will help you retrieve specific data from your database throughout your app.

To create a new QueryBuilder, you need to pass as a parameter the desired ParseObject subclass, which is the one that will contain your query results.

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.

Dart


You can read more about the QueryBuilder class here at the official documentation.

Using the JavaScript Console on Back4App

Inside your Back4App application’s dashboard, you will find a very useful API console in which you can run JavaScript code directly. In this guide you will use to store data objects in Back4App. On your App main dashboard go to Core->API Console->Javascript.

Document image


2 - Save Data on Back4app

To run the queries on this guide you’ll need first to populate your App with some data.

The classes are: Author, Book, Publisher and BookStore. Which Book has a 1:N relation with Publisher and N:N with Author, and BookStore has an N:N relation with Book.

Here is the Parse.Object classes creation code, so go ahead and run it in your API console:

Dart


After running this code, you should now have a Author, Publisher, Book and BookStore class in your database.

Your new class should look like this:

Document image


Let’s now take a look at examples from every QueryBuilder method, along with brief explanations on what they do.

3 - Query the data

Now that you have a populated class, we can now perform some relational queries

Now that you have populated all the classes, we can now perform some relational queries in it.

Let’s begin by filtering Book results by the publisher, searching for the ones that belong to the Publisher “Acacia Publishings” (or “Publisher A”) with relational Pointer using the basic .whereEqualTo method:

Dart


Let’s now query which BookStore objects contain Book objects with publishing date greater than 01/01/2010, using an inner query with the whereGreaterThan method and then the whereMatchesQuery method:

Dart


Let’s now create another query, looking for Authors that are relation with the book Can You Believe It?, using whereRelatedTo:

Dart


4 - Query from Flutter

Let’s now use our example queries inside a Flutter App, with a simple interface having a list showing results and also 3 buttons for calling the queries.

Open your Flutter project, go to the main.dart file, clean up all the code, and replace it with:

Dart


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 load as shown in the image.

Document image


Conclusion

At the end of this guide, you learned how relational queries work on Parse and how to perform them on Back4App from a Flutter App. In the next guide, you will learn how to work with Users in Parse.

Updated 03 Jun 2024
Did this page help you?