Flutter fromJson() to convert json to class object | Dart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone assalamualaikum here we'll  see how to use named constructor from json   so from json is a conventional name for  a named constructor in dart or flatter   and we'll see how to use that and when to use  that so first let's gets familiar with a rest api   call which is this one now this one returns a json  so in our example we'll use this uri and this uri   will return a json well i already have this  uri over here and i'm using http package   for our http client and i'm using a get method so  all i'm doing over here first i'm passing this uri   using this uri dot parse so it will return me a  response well to use this json response in our   application either it's here or in flutter the  idea is same first we need to create a class or   a model based on this json response now right now  we'll see that over here we have four fields but   in our case to be simple we'll only use this two  fields so now we'll go ahead and use these two   fields to create a class or a model so here is  my class or a lot of time you'll call it model   and here i have this two variables or fields  title and body which corresponds to these two and then i have a default constructor and then i  have another constructor which is called from.json   this is called named constructor  okay now over here i'm passing a json   and i'm pressing this json and each of the item in  the json i'm assigning to them a certain field in   this class now we want to call this constructor  from json from here and as we call it we need to   pass a map object over here as you can see okay  it will return the response in this rest variable and then we take this rest variable and we take  the body of this here rest returns adjacent string   and we are taking the body of it now see we wrap  it around using json decode because we want to   make sure that even though this is json string  but we want to confirm that this is a json or a   map so we can only confirm using this one json  decode because from json takes a json or a map   okay so then you can go ahead and print the  body or do whatever you want or store it   somewhere else so that you can use in in your  app later now we'll go ahead and print the body so here you see this is the body same  as exactly the one we get from here and at the same time we can also print the title and you see that i told the one we had earlier  over here so the idea is if you have a network   request and you get a response from the network  most probably you should take the body of it and   then use json decode to convert it to json and  then most probably you should call from json   and you call from json to convert it to an object  so that you can get the underline property or   methods using this dot property or dot operator so  this is where you use from json thank you so much
Info
Channel: dbestech
Views: 14,014
Rating: undefined out of 5
Keywords: dart fromJson to class objects, dart convert json to object, dart json parser, dart http request json, programming, flutter tutorial, flutter
Id: dCIOabA6Fa0
Channel Id: undefined
Length: 3min 46sec (226 seconds)
Published: Fri Apr 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.