Serverpod with Flutter | CRUD operations tutorial (Part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now these two lines of code is all you need to fetch records from database [Music] welcome back once again I'm your host afzil and in this video we're gonna talk about server pod crud operation in the last video we have discussed what is server pod and what are the installation you need to do to get started so if you don't want to get stuck anywhere make sure you watch that video first and let's get started with today's tutorial you need to have Docker instance running in the background to create a new project and then head over terminal and you have to write this command which is server pod create and then name of your project after the command is successfully executed you will have three projects one is for flutter one is for server and one is the intermediate client project so let's head over to the server project and start creating our first API now if you have seen my previous video where I talk about server pod I have explained in detail what are these different files and folder structure means so let's head over to endpoints folder and create a new endpoint for our articles so you just need to extend your class with endpoint and that will give you all sort of feature to expose it as a client method now let's go ahead and create our first method and as a side note each method should have a return type of future and when you are working with future it should be asynchronous method and also it is worth noting that first parameter of each method is session and don't worry you don't have to pass this session from your client it is automatically done and by the way this session comes from serverpot.dart if it didn't automatically pick for you so this is typically you write a method inside your endpoint now of course we have to deal with the article so we need to replace this string with our custom model for that purpose we have to go to protocol folder and create article class so this is going to be a yaml file so let's have a look how to create your yaml for custom model you have to define a keyword class and then colon and name of the class it's just a typical yaml you use for pubs pick in the same fashion you have to Define all the properties so if you want to create a table for this article you define the name of the table and all the fields required in that class it supports Boolean integer string date time and byte data out of the box and once you are done defining the ml file you go ahead and run this command which will generate all the class tables and everything you need to perform the database operation I know it's too much to consume in one video all this information but bear with me and anytime if you feel like you can go ahead and watch the tutorial watch the particular section again to understand better how things are working now here's the generated file you can see there's a class article there's a table name and serializable deserializable all the properties all the operation insert delete find and everything you need to perform any action is already there in this class file Now using the same Auto generated class we are going to find all the records and you just have to say find it will take parameter as session which is already there and here you have future of list of Articles so we have to first wait for the result and then change the data type return type to list of article now these two lines of code is all you need to fetch records from database and optionally if you want to perform some other operation like if you want to search for any particular record you can pass that keyword or that parameter over here and it works just like any dot method you have optional parameter you have null safety in place so you feel at home all the time and inside the same find method there's a where parameter where you can perform all the SQL operation now you don't have to write SQL query for that you just use any of the property and then Define your condition like equal to greater than and all sort of expression that you have inside SQL now because our keyword can be null so let's put a null check over here if it is not null then perform a like query otherwise just treat this condition as true all the time now on the same note let's also go ahead and create a method for adding article and similarly we will have a session as the first parameter and we'll accept article from the user which we want to insert into database you might have guessed how easy it is going to be you just have to say article dot insert and provide the parameter which you just accepted and that's all you need to do to insert a record inside database and quickly I am going to copy paste the delete method it's quite the same you just have to write a condition what item to delete so now when we are done with all the development work let's go ahead and run the server for that you have to write two commands first is to make the docker instance up and running you can write Docker compose up parameters build and detach now if you are thinking what this command does it starts the postgres DB and the red is in memory database but still our Dart code is not running so for that purpose you have to give this command Dart and provide the path of main file and then hit enter what it will do it will run all of your Dart code and it will host it on 8080 Port so if you open a Google Chrome browser you should see something like this okay message that means your server is up and running your postgres your radius everything is up and running and you are good to consume This Server inside your client application and before we move to the client project there's one last thing remaining we need to register our table inside database don't worry all the script all the SQL part is ready for you you just have to copy paste so for that purpose first you need to establish connection with your database and you can find all those information of your database inside the config folder so go to the respective environment currently I am in development so I will just connect to the local database okay the user is postgres the name of the database is article the it's running on the port 8090 so all the information you can find inside the respective environment file again which is a yaml file and for the database password you can head over to password.yaml which contains all the password for all the environments so here you can visualize all the tables which comes along with server pod so you can create your own table over here now don't worry you have the script with you so you head to generated folder and then there's something called tables.pgsql just copy everything from this file if you have multiple tables you're going to have multiple SQL query over here so just copy everything and paste inside post Echo SQL editor and select everything again and then execute the selection and after that you just have to reload the schema with command r or control r on Windows and here you have the article table now remember that this whole thing with post Eco is just one time setup until unless you change the schema of your table at this point we are all done with the server side we have the table created to perform operation we have the docker images up and running we have the dart server up and running so now it's time to move to the flutter project and start integrating our server code click on the thumbnail in the middle of the screen to watch the next video
Info
Channel: CodeX
Views: 9,647
Rating: undefined out of 5
Keywords: flutter, tutorial, how to, android, ios, ui, serverpod, serverpod for flutter, flutter with serverpod, endpoints in serverpod, postgres db, postico, crud operations, with serverpod, dart, dart flutter, flutter backend, dart backend
Id: 93NgZLDB5pM
Channel Id: undefined
Length: 8min 3sec (483 seconds)
Published: Sat Oct 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.