Query Users
Some applications need to directly manage users or be able to view a list of them. Parse has query tools and they can be used to list the users of your application.
In this guide, you will learn how to use ParseQuery to perform user queries in your Flutter application using the Flutter plugin for Parse Server.
To build a user querying feature using Parse for a Flutter App.
To complete this tutorial, you will need:
- 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.
To better understand the Query Users process, we will create an app to query. We won’t explain the Flutter application code once this guide’s primary focus is using the Flutter with Parse.
Following the next steps, you will build a Todo App that will store the tasks at Back4App Database.
Following the next steps you will be able to build a Sign App that will create user Account in Back4App Database.
Open your Flutter project from the previous guide Flutter plugin for Parse Server. Go to the main.dart file, clean up all the code, and replace it with:
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.
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.
Any Parse query operation uses the ParseQuery object type, which will help you retrieve specific data from your database throughout your app.
A ParseQuery will only resolve after calling a retrieve method, so you can set up a query and chain its several modifiers before submitting the retrieve method.
To create a new ParseQuery, you need to pass as a parameter the desired ParseObject subclass, which is the one that will contain your query results.
You can see a user query example below. Using the code provided, find the doUserQuery function in the file main.dart. Replace the code inside doUserQuery with:
To build this function, follow these steps:
- Create an instance ofParseQuery class e pass as a parameter to the ParseUser.forQuery
- Call thequery function that will execute the query against the database.
- If the operations succeed, will return a list ofParseUser objects. If the operation does not find any objects, the success property will be false, and the results are null.
The complete code should look like this:
You can also try to retrieve a single user using the following structure:
To test it, click on the Run button in Android Studio/VSCode.
After performing this query, your user list on your app should be showing something like this:
At the end of this guide, you learned how to perform queries on Parse users on Flutter.