Flutter
...
Authentication
Query Users
11 min
querying users in parse on flutter introduction 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 goal to build a user querying feature using parse for a flutter app prerequisites to complete this tutorial, you will need flutter version 2 2 x or later https //flutter dev/docs/get started/install android studio https //developer android com/studio or vs code installed (with plugins dart and flutter) an app created on back4app note follow the new parse app tutorial to learn how to create a parse app 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 understanding the query users app 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 let’s get started! following the next steps you will be able to build a sign app that will create user account in back4app database 1 create query users app template open your flutter project from the previous guide flutter plugin for parse server go to the main dart main dart file, clean up all the code, and replace it with 1 import 'dart\ async'; 2 3 import 'package\ flutter/material dart'; 4 import 'package\ parse server sdk flutter/parse server sdk dart'; 5 6 void main() async { 7 widgetsflutterbinding ensureinitialized(); 8 9 final keyapplicationid = 'your app id here'; 10 final keyclientkey = 'your client key here'; 11 final keyparseserverurl = 'https //parseapi back4app com'; 12 13 await parse() initialize(keyapplicationid, keyparseserverurl, 14 clientkey keyclientkey, debug true); 15 16 runapp(materialapp( 17 home home(), 18 )); 19 } 20 21 class home extends statefulwidget { 22 @override 23 homestate createstate() => homestate(); 24 } 25 26 class homestate extends state\<home> { 27 final scaffoldkey = globalkey\<scaffoldstate>(); 28 29 @override 30 widget build(buildcontext context) { 31 return scaffold( 32 appbar appbar( 33 title text("parse query users"), 34 backgroundcolor colors blueaccent, 35 centertitle true, 36 ), 37 key scaffoldkey, 38 body futurebuilder\<list\<parseobject>>( 39 future douserquery(), 40 builder (context, snapshot) { 41 switch (snapshot connectionstate) { 42 case connectionstate none 43 case connectionstate waiting 44 return center( 45 child container( 46 width 100, 47 height 100, 48 child circularprogressindicator()), 49 ); 50 default 51 if (snapshot haserror) { 52 return center( 53 child text("error ${snapshot error tostring()}"), 54 ); 55 } else { 56 if (snapshot data! isempty) { 57 return center( 58 child text('none user found'), 59 ); 60 } 61 62 return listview\ builder( 63 padding edgeinsets only(top 10 0), 64 itemcount snapshot data! length, 65 itembuilder (context, index) { 66 final user = snapshot data!\[index] as parseuser; 67 final userverified = user emailverified ?? false; 68 return listtile( 69 title text( 70 'username ${user username} verified ${userverified tostring()}'), 71 subtitle text(user createdat tostring()), 72 ); 73 }); 74 } 75 } 76 })); 77 } 78 79 future\<list\<parseobject>> douserquery() async { 80 return \[]; 81 } 82 } 83 when debug debug parameter in function parse() initialize parse() initialize is true 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 2 connect template to back4app project find your application id and client key credentials navigating to your app dashboard at back4app website https //www back4app com/ update your code in main dart 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 3 code for query users any parse query operation uses the parsequery parsequery object type, which will help you retrieve specific data from your database throughout your app a parsequery 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 parsequery , you need to pass as a parameter the desired parseobject 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 douserquery function in the file main dart main dart replace the code inside douserquery douserquery with 1 querybuilder\<parseuser> queryusers = 2 querybuilder\<parseuser>(parseuser forquery()); 3 final parseresponse apiresponse = await queryusers query(); 4 5 if (apiresponse success && apiresponse results != null) { 6 return apiresponse results as list\<parseobject>; 7 } else { 8 return \[]; 9 } to build this function, follow these steps create an instance of parsequery parsequery class e pass as a parameter to the parseuser forquery parseuser forquery call the query query function that will execute the query against the database if the operations succeed, will return a list of parseuser parseuser 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 1 future\<list\<parseobject>> douserquery() async { 2 querybuilder\<parseuser> queryusers = 3 querybuilder\<parseuser>(parseuser forquery()); 4 final parseresponse apiresponse = await queryusers query(); 5 6 if (apiresponse success && apiresponse results != null) { 7 return apiresponse results as list\<parseobject>; 8 } else { 9 return \[]; 10 } 11 } you can also try to retrieve a single user using the following structure to test it, click on the run run button in android studio/vscode after performing this query, your user list on your app should be showing something like this it’s done! at the end of this guide, you learned how to perform queries on parse users on flutter