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 \<font color="#2166ae">parseobject\</font> can be used as a value in other \<font color="#2166ae">parseobject\</font> 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 \<font color="#2166ae">one to one\</font> , establishing direct relations between two objects and only them; \<font color="#2166ae">one to many\</font> , where one object can be related to many other objects; \<font color="#2166ae">many to many\</font> , which can create many complex relations between many objects in this guide we will detail how the \<font color="#2166ae">one to many\</font> relation works using a pratical app example there are two ways to create a \<font color="#2166ae">one to many\</font> relation in parse the first is using the \<font color="#2166ae">pointers\</font> in \<font color="#2166ae">child class\</font> , which is the fastest in creation and query time the second is using \<font color="#2166ae">arrays\</font> of \<font color="#2166ae">pointers\</font> 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 \<font color="#2166ae">pointers\</font> relation as \<font color="#2166ae">one to one\</font> 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 https //code visualstudio com/ (with plugins https //docs flutter dev/get started/editor dart and flutter) an app created https //www back4app com/docs/get started/new parse app on back4app note follow the new parse app tutorial https //www back4app com/docs/get started/new parse app 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 https //www back4app com/docs/flutter/parse sdk/parse flutter sdk 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 \<font color="#2166ae">book\</font> class, storing each book entry in the registration also, these are the other three object classes \<font color="#2166ae">publisher\</font> book publisher name, one to many relation with \<font color="#2166ae">book\</font> ; \<font color="#2166ae">genre\</font> book genre, one to many relation with \<font color="#2166ae">book\</font> note that for this example we will consider that a book can only have one genre; \<font color="#2166ae">author\</font> book author, many to many relation with \<font color="#2166ae">book\</font> , 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 ( \<font color="#2166ae">publisher\</font> , \<font color="#2166ae">genre\</font> ) has only a string type \<font color="#2166ae">name\</font> attribute and \<font color="#2166ae">book\</font> has \<font color="#2166ae">title\</font> and \<font color="#2166ae">year\</font> , apart from any additional relational attribute in the previous guides we have already seen how to save and read \<font color="#2166ae">parseobject\</font> so in this guide, we will not cover how to save and read \<font color="#2166ae">genre\</font> and \<font color="#2166ae">publisher\</font> 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 \<font color="#2166ae">debug\</font> parameter in function \<font color="#2166ae">parse() initialize\</font> is \<font color="#2166ae">true\</font> , 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 \<font color="#2166ae">main dart\</font> 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 \<font color="#2166ae">add genre\</font> to register and view the list of \<font color="#2166ae">genres\</font> that will be used in the registration of books click on \<font color="#2166ae">add publisher\</font> to register and view the list of \<font color="#2166ae">publishers\</font> that will be used in the registration of books click on \<font color="#2166ae">add book\</font> to register new \<font color="#2166ae">book\</font> using relations with \<font color="#2166ae">genre\</font> and \<font color="#2166ae">publisher\</font> click on \<font color="#2166ae">list publisher/book\</font> to view the list of \<font color="#2166ae">publishers\</font> and \<font color="#2166ae">books\</font> 3 save a book object and its relations this function will create a new book in back4app database with relations search for the function \<font color="#2166ae">dosavebook\</font> in file \<font color="#2166ae">main dart\</font> , and insert the code below inside the \<font color="#2166ae">future\<void\> dosavebook()\</font> 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 \<font color="#2166ae">book\</font> class with the command \<font color="#2166ae">parseobject('book')\</font> 2 use the \<font color="#2166ae">set\</font> function to set the fields for this object 2 1 \<font color="#2166ae">title\</font> is a text attributes that receive value from the text controller 2 2 \<font color="#2166ae">genre\</font> receives the value by defining a \<font color="#2166ae">parseobject\</font> with the \<font color="#2166ae">objectid\</font> of the selected \<font color="#2166ae">genre\</font> ( parse will convert to pointer on save ) 2 3 \<font color="#2166ae">publisher\</font> receives the value by defining a \<font color="#2166ae">parseobject\</font> with the \<font color="#2166ae">objectid\</font> of the selected \<font color="#2166ae">publisher\</font> ( note that we can specify for parse that we want to save as a \<font color="#2166ae">pointer\</font> using the \<font color="#2166ae">topointer()\</font> method ) 3 call the \<font color="#2166ae">save\</font> function in \<font color="#2166ae">parseobject\</font> , which will effectively register the object to your database in the back4app dashboard run the app and test the new function click on the \<font color="#2166ae">add book\</font> 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 \<font color="#2166ae">save book\</font> button to confirm that the new object is save in the database with relations, you can access the \<font color="#2166ae">back4app dashboard\</font> and access \<font color="#2166ae">book\</font> 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 \<font color="#2166ae">getbooklist\</font> in the file \<font color="#2166ae">main dart\</font> , then replace the code below inside \<font color="#2166ae">future\<list\<parseobject\>\> getbooklist(string publisherid)\</font> 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 \<font color="#2166ae">parsequery\</font> object for \<font color="#2166ae">book\</font> class insert a condition in the query, to search \<font color="#2166ae">books\</font> where \<font color="#2166ae">publisher\</font> field is equal \<font color="#2166ae">pointer\</font> of \<font color="#2166ae">publisher parseobject\</font> we sort the result in ascending \<font color="#2166ae">name\</font> order do a query’s search method using \<font color="#2166ae">query()\</font> method if the operations succeed, objects in \<font color="#2166ae">book\</font> will be returned run the app and test the new query first, click on the \<font color="#2166ae">list publisher/book\</font> button it’s done! at this point, you learned how to create and query one to many relations in parse on \<font color="#2166ae">flutter\</font> 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