Flutter
...
Data Objects
1:N Relationship
16 min
one to many relationship on flutter introduction using parse, you can store data objects establishing relations between them to model this behavior, any parseobject parseobject can be used as a value in other parseobject parseobject 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 there are three main relation types one to one one to one , establishing direct relations between two objects and only them; one to many one to many , where one object can be related to many other objects; many to many many to many , which can create many complex relations between many objects in this guide we will detail how the one to many one to many relation works using a pratical app example there are two ways to create a one to many one to many relation in parse the first is using the pointers pointers in child class child class , which is the fastest in creation and query time the second is using arrays arrays of pointers pointers in parent class which can lead to slow query times depending on their size because of this performance issue, we will use only pointers examples you will implement a flutter book registration app and will create and query related objects using the parse pointers pointers relation as one to one one to one is not common and we are not going to cover on our guides as an example a relationship between the user class and another class that will contain sensitive user data for security reasons https //blog back4app com/parse server best practices/ ( 1 4 don’t let users have access to sensitive data from others ) prerequisites 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 book app the main object class you’ll be using is the book book class, storing each book entry in the registration also, these are the other three object classes publisher publisher book publisher name, one to many relation with book book ; genre genre book genre, one to many relation with book book note that for this example we will consider that a book can only have one genre; author author book author, many to many relation with book book , since a book can have more than one author and an author can have more than one book as well; a visual representation of these data model we will assume that each object class ( publisher publisher , genre genre ) has only a string type name name attribute and book book has title title and year year , apart from any additional relational attribute in the previous guides we have already seen how to save and read parseobject parseobject so in this guide, we will not cover how to save and read genre genre and publisher publisher objects you will find the following screens on the book app registration and listing of genre registration and list of publishers book registration list of publishers and books book details we won’t explain the flutter application code once this guide’s primary focus is using the flutter with parse using relations 1 create book app template let’s first run the book app project template open your flutter project from the previous guide flutter plugin for parse server the book flutter app https //github com/templates back4app/flutter associations repository is also available to you clone and run the project copy the main dart https //github com/templates back4app/flutter associations/blob/master/lib/main dart file and replace your current code from previous guides note 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 step 2 connect template to back4app project find your application id and client key credentials navigating to your app dashboard at back4app dashboard >app settings >security & keys https //www back4app com/docs/parse dashboard/app settings 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 click on add genre add genre to register and view the list of genres genres that will be used in the registration of books click on add publisher add publisher to register and view the list of publishers publishers that will be used in the registration of books click on add book add book to register new book book using relations with genre genre and publisher publisher click on list publisher/book list publisher/book to view the list of publishers publishers and books books 3 save a book object and its relations this function will create a new book in back4app database with relations search for the function dosavebook dosavebook in file main dart main dart , and insert the code below inside the future\<void> dosavebook() future\<void> dosavebook() function 1 final book = parseobject('book') 2 set('title', controllertitle text trim()) 3 set('year', int parse(controlleryear text trim())) 4 //the objectid will be converted to a pointer on the save() method 5 set('genre', parseobject('genre') objectid = genre objectid) 6 //you can also convert to a pointer object before the saving using the topointer() method 7 set( 8 'publisher', 9 (parseobject('publisher') objectid = publisher objectid) 10 topointer()); 11 12 await book save(); to build this function, follow these steps 1\ create a new instance of the parse book book class with the command parseobject('book') parseobject('book') 2 use the set set function to set the fields for this object 2 1 title title is a text attributes that receive value from the text controller 2 2 genre genre receives the value by defining a parseobject parseobject with the objectid objectid of the selected genre genre ( parse will convert to pointer on save ) 2 3 publisher publisher receives the value by defining a parseobject parseobject with the objectid objectid of the selected publisher publisher ( note that we can specify for parse that we want to save as a pointer pointer using the topointer() topointer() method ) 3 call the save save function in parseobject parseobject , which will effectively register the object to your database in the back4app dashboard run the app and test the new function click on the add book add book button fill book information the app requires the selection of the authors (s), but the code for them will be covered only in the next guide click on save book save book button to confirm that the new object is save in the database with relations, you can access the back4app dashboard back4app dashboard and access book book class if you access your book class using the dashboard you can click on the object pointer value and you will be redirected to the referenced object it may seem like a harmless feature, but this makes debugging and error tracing much quicker than searching for it manually 4 query the book list and its related objects this function will query books in back4app database with using relations with publisher through the publisher, we will get the list of books search for the function getbooklist getbooklist in the file main dart main dart , then replace the code below inside future\<list\<parseobject>> getbooklist(string publisherid) future\<list\<parseobject>> getbooklist(string publisherid) function 1 querybuilder\<parseobject> querybook = 2 querybuilder\<parseobject>(parseobject('book')) 3 whereequalto('publisher', 4 (parseobject('publisher') objectid = publisherid) topointer()) 5 orderbyascending('title'); 6 final parseresponse apiresponse = await querybook query(); 7 8 if (apiresponse success && apiresponse results != null) { 9 return apiresponse results; 10 } else { 11 return \[]; 12 } to build this function, follow these steps create an instance of parsequery parsequery object for book book class insert a condition in the query, to search books books where publisher publisher field is equal pointer pointer of publisher parseobject publisher parseobject we sort the result in ascending name name order do a query’s search method using query() query() method if the operations succeed, objects in book book will be returned run the app and test the new query first, click on the list publisher/book list publisher/book button it’s done! at this point, you learned how to create and query one to many relations in parse on flutter flutter in the next guide, we’ll show you how to make many to many relationships and how to perform queries returning data from related objects