How to convert Json file to dart class

When you are getting the data from API, the data is in the form of JSON.

For example we will take student information as a JSON file, which contains student id, name and address

To use in our flutter project we need to create a student class which is having the arguments like student id, name and address

For this we have to parse the JSON data and added to our student object

But if we have more JSON files then we have to write the code for all the classes, instead of that we can use one simple process is nothing but JSON serializable

For this we need to add 3 packages one is for seriazation and another one is for annotations and the last one is for build runner

Now, we are at climax, here what we have to do is

So, here we are adding user.g.part is nothing but part file of original User class file, and adding two methods fromJson and ToJson.

And finally, we have to run a command in terminal like below

flutter packages pub run build_runner build

It will generates the dart class, That’s it.

Thanks for reading, if you like my article please applaud.

--

--

--

Mobile App Developer and Trainer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

OPEN WEATHER API WITH FLUTTER APP

Visual Studio

How I Transitioned from Ease to Spring Animations

Best Custom 404 Page Templates

Coding Space Invaders in JavaScript Complete Tutorial Every Step Explained with HTML5 Canvas

Adding Tailwind to Eleventy

Communicate between independent components in Reactjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Satheesh Guduri

Satheesh Guduri

Mobile App Developer and Trainer

More from Medium

BLoC pattern: Learn in painless way

Setting up Android Studio for Flutter

Deploying flutter apps to the PlayStore

Flutter Navigation Guide - Push(), Pop() with all required method variants.