To make a big application, we always need some rest API's to provide a dynamic feature to the user. These API often supports JSON as a data format for communication through HTTP protocol.
If you have worked with native Android application development. You definitely missing those POJO classes in Flutter. After hit a request to the server, the server will return a JSON string. If you want to use data flexibly, we need to convert the JSON string into an object.
Are you going to use GSON equivalent libs in Flutter?
No, GSON and other equivalent libs used runtime reflection, which is disabled in Flutter. The runtime reflection interferes with tree shaking, which Dart has supported for quite a long time. Tree shaking helps us to remove unused code from your release builds. This optimizes the app’s size significantly. The reflection makes all code implicitly used by default, it makes tree shaking difficult. The tools cannot know what parts are unused at runtime, so the redundant code is hard to strip away. App sizes cannot be easily optimized when using reflection.
So, Flutter only provides map for parse a JSON with the help of dart:convert. It is a handwritten deserialization that is extremely unstable in large projects. The manual parsing method, we have discussed in our Parse local JSON file in Flutter post. This is the most basic parsing method and it is only recommended if you are starting with Flutter or you’re building a small project. But if you need to work with a larger project. You must use the following method that we going to explain here.
In this post, we will use json_annotation, build_runner, and json_serializable to parse a big JSON in the Flutter Application that is recommended method by Flutter Team. For the example simplicity, we'll use the following json because we not getting data from the API.
Let's create a flutter sample app for parse above JSON file and use above libs.
Creating a new Project
1. Create a new project from File ⇒ New Flutter Project with your development IDE.
2. Create a assets folder in the root directory of the project and put book.json in the assets folder and paste above json in this file. To access this file in the Flutter application, we have to declare it in the pubspec.yaml file. As we have done:
3. Now we need to write all dart entity classes manually based on above JSON data. As you can see below, we have written @JsonSerializable() in PODO class. It'll help us to auto-generate serialize code in the Flutter. All the members of a class is the same as written in the JSON raw data file. You can change it according to your requirement but that case, you have to use @JsonKey annotation with each member like if I'll change per_page to perPage then I'll write it such as:
@JsonKey(name: 'per_page ')
int perPage;
But in this post, i'm not doing it as you can see below written enties book.dart, author.dart and user.dart files.
4. To generate PODO for the above enties, we have to run the command.
flutter packages pub run build_runner build
as i have run it in the Android Studio terminal.
the above script will generate book.g.dart, author.g.dart, and user.g.dart. It'll auto map with your manual written classes. If it shows any error. Then you have to add part 'book.g.dart'; in book.dart and part of 'book.dart'; in book.g.dart files respectively to resolve error.
as you can see above. Our PODO is ready for parse a json string.
5. Now, let's see how to start parse json and create a object. To create a object of json, we getting json string from local asset folder of the project.
the above snippet return a json string. To parse it we have to call root entity method.
here, we have final code snippet of this project. Where we have used some Flutter widgets to display content of json.
Now run the above snippet and you will the following output of project and it's showing above json file data.
If you have followed the article carefully, you can see the app running very smoothly as shown in the above. But if you are facing any problem or you have any quires, please feel free to ask it from below comment section and you can ask it on SLACK.
If you have worked with native Android application development. You definitely missing those POJO classes in Flutter. After hit a request to the server, the server will return a JSON string. If you want to use data flexibly, we need to convert the JSON string into an object.
Are you going to use GSON equivalent libs in Flutter?
No, GSON and other equivalent libs used runtime reflection, which is disabled in Flutter. The runtime reflection interferes with tree shaking, which Dart has supported for quite a long time. Tree shaking helps us to remove unused code from your release builds. This optimizes the app’s size significantly. The reflection makes all code implicitly used by default, it makes tree shaking difficult. The tools cannot know what parts are unused at runtime, so the redundant code is hard to strip away. App sizes cannot be easily optimized when using reflection.
So, Flutter only provides map for parse a JSON with the help of dart:convert. It is a handwritten deserialization that is extremely unstable in large projects. The manual parsing method, we have discussed in our Parse local JSON file in Flutter post. This is the most basic parsing method and it is only recommended if you are starting with Flutter or you’re building a small project. But if you need to work with a larger project. You must use the following method that we going to explain here.
In this post, we will use json_annotation, build_runner, and json_serializable to parse a big JSON in the Flutter Application that is recommended method by Flutter Team. For the example simplicity, we'll use the following json because we not getting data from the API.
Let's create a flutter sample app for parse above JSON file and use above libs.
Creating a new Project
1. Create a new project from File ⇒ New Flutter Project with your development IDE.
2. Create a assets folder in the root directory of the project and put book.json in the assets folder and paste above json in this file. To access this file in the Flutter application, we have to declare it in the pubspec.yaml file. As we have done:
3. Now we need to write all dart entity classes manually based on above JSON data. As you can see below, we have written @JsonSerializable() in PODO class. It'll help us to auto-generate serialize code in the Flutter. All the members of a class is the same as written in the JSON raw data file. You can change it according to your requirement but that case, you have to use @JsonKey annotation with each member like if I'll change per_page to perPage then I'll write it such as:
@JsonKey(name: 'per_page ')
int perPage;
But in this post, i'm not doing it as you can see below written enties book.dart, author.dart and user.dart files.
4. To generate PODO for the above enties, we have to run the command.
flutter packages pub run build_runner build
as i have run it in the Android Studio terminal.
the above script will generate book.g.dart, author.g.dart, and user.g.dart. It'll auto map with your manual written classes. If it shows any error. Then you have to add part 'book.g.dart'; in book.dart and part of 'book.dart'; in book.g.dart files respectively to resolve error.
as you can see above. Our PODO is ready for parse a json string.
5. Now, let's see how to start parse json and create a object. To create a object of json, we getting json string from local asset folder of the project.
the above snippet return a json string. To parse it we have to call root entity method.
here, we have final code snippet of this project. Where we have used some Flutter widgets to display content of json.