Flutter
...
Data Objects
N:N Relationship
11 min
many to many relationship on flutter introduction in the previous guide, we learned how to use one to many relations and we will continue with our project in this guide, we will focus on the most common relation many to many there are three ways to create a many to many relation in parse the first is using the parse relations relations , which is the fastest in creation and query time we will use this in this guide the second is using arrays arrays of pointers pointers which can lead to slow query times depending on their size the third is using jointable jointable where the idea from classical database when there is a many to many relation, we combine every objectid objectid or pointer pointer from both sides together to build a new separate table in which the relationship is tracked in this guide you will implement a many to many many to many relationship on a flutter book registration app using the parse relations relations you will learn how to create and query many to many data relations and how to perform queries returning data from related objects, using back4app and the flutter sdk prerequisites android studio https //developer android com/studio or vs code installed (with plugins dart and flutter) note the flutter app created in previous guide complete the previous guide so you can have a better understanding of the one to may relationship one to may relationship class a device (or virtual device) running android or ios 1 run the book app template if you have not completed the previous guide you can clone and run the complete book flutter app https //github com/templates back4app/flutter associations project from our repository you can also take a look at the previous guide https //www back4app com/docs/flutter/parse sdk/data objects/%7bsite baseurl%7d%7d//flutter/parse sdk/data objects/flutter one to many relationship to better understand the app template below you can find a visual representation of book registration data model 2 save a book object and its author’s open the flutter project from the previous guide one to many relationship on flutter https //www back4app com/docs//flutter/parse sdk/data objects/flutter one to many relationship search for the function dosavebook dosavebook in file main dart main dart , and replace with the code below inside the future\<void> dosavebook() future\<void> dosavebook() function below this function will create a new book in back4app data store with relations 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('publisher', 8 (parseobject('publisher') objectid = publisher objectid) topointer()) 9 //saving a list of authors for the book 10 addrelation( 11 'authors', 12 authors 13 map((o) => parseobject('author') objectid = o objectid) 14 tolist()); 15 16 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 ) 2 4 authors authors we call the addrelation addrelation method of parseobject parseobject , sending a list of parseobject parseobject with the objectid objectid of the selected authors authors 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 dosavebook() dosavebook() function first, access the dashboard and delete the books that were previously registered in the previous guide click on the add book add book button fill book information with authors 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 clicking on the object pointer/relation value in your dashboard will take you to the referenced object entry it may seem like a harmless feature, but this makes debugging and error tracing much quicker than searching for it manually 3 query the book details with relations this function will query book details in back4app database, returning relationship data in some situations, you want to return multiple types of related objects in one query you can do this with the includeobject includeobject method in our example, we want to return the books, with information from genre and publishers search for the function getbookdetail getbookdetail in the file main dart main dart , then replace the code below inside getbookdetail(parseobject book) getbookdetail(parseobject book) function 1 querybuilder\<parseobject> querybook = 2 querybuilder\<parseobject>(parseobject('book')) 3 whereequalto('objectid', book objectid) 4 includeobject(\['publisher', 'genre']); 5 6 final parseresponse responsebook = await querybook query(); 7 8 if (responsebook success && responsebook results != null) { 9 final book = (responsebook results first) as parseobject; 10 booktitle = book get\<string>('title'); 11 bookyear = book get\<int>('year'); 12 bookgenre = book get\<parseobject>('genre') get\<string>('name'); 13 bookpublisher = book get\<parseobject>('publisher') get\<string>('name'); 14 loadeddata = true; 15 } 16 17 querybuilder\<parseobject> queryauthors = 18 querybuilder\<parseobject>(parseobject('author')) 19 whererelatedto('authors', 'book', book objectid); 20 21 final parseresponse responseauthors = await queryauthors query(); 22 23 if (responseauthors success && responseauthors results != null) { 24 bookauthors = responseauthors results 25 map((e) => (e as parseobject) get\<string>('name')) 26 tolist(); 27 } 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 objectid objectid field is equal objectid objectid of the selected book we use the includeobject method, informing the fields of the pointers that we want to return the data in the same query genre and publisher you can also do multi level includeobject using dot notation exemple ` includeobject(\[‘post’, ‘post authors’]); do a query’s search method using query() query() method if the operations succeed, object in book book will be returned we use the get method to retrieve the data for fields that are pointers, we will first need to retrieve the pointer, then obtain its data example bookgenre = book get\<parseobject>('genre') get\<string>('name'); bookgenre = book get\<parseobject>('genre') get\<string>('name'); in the second stage of processing, we need to recover the authors associated with the book to build this function, follow these steps create an instance of parsequery parsequery object for authors authors class insert a condition in the query, using whererelatedto whererelatedto operator to search authors authors relationship with book book , where book book is equal objectid objectid of the selected book do a query’s search method using query() query() method if the operations succeed, object in book book will be returned we use the get method to retrieve the data run the app and test the new query first, click on the list publisher/book list publisher/book button select a book from the list the next screen will display the data for the books and their relationships it’s done! at this point, you learned how to create and query many to many relations and how to perform queries returning data from related objects in parse on flutter flutter