To make a big mobile application, we always need API's to get dynamic JSON data from the server. JSON is text-based, human-readable data interchange format that is used for representing simple data structures and objects. By using text-based JSON, we can create a list of objects in mobile and web application.
But sometimes in the mobile applications, we have to put JSON file in the application package rather than get it from the server. In the mobile application, we can put it in anywhere in the application directory as per platform guideline. Such as in Android and Flutter, we can put a JSON file in the assets folder and parse it on runtime to get the data and use it accordingly.
In this post, we'll create a Flutter application and see how to parse a JSON locally within our app. The final output of app looks like below:
Let's dive into Flutter app development with the help of following steps.
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 country.json in the assets folder. To access this file in the Flutter application, we have to declare it in the pubspecs.yaml file. As we have did:
3. After that, create a list.dart file. In this file, we'll pass a list of countries array as a param and displaying it in the build method by using the listview.
4. Now, create a PODO model country.dart file. It'll represent a country detail that we used to parse JSON.
5. At the end, open main.dart file and write main logic to get JSON from assets folder and parse it for display. As you can see below, to get json from the assets folder, we using the DefaultAssetBundle widget.
in the builder param snapshot.data.toString() represent a string of JSON. We pass it to parseJson() method for parse it.
the parseJson() method make a list of countries.
Here, you can see the complete main.dart file. Now merge all above snippet and run the app.
If you have followed the article carefully, you can see the app running very smoothly as shown in the above. The raw JSON displaying a list of countries. But if you are facing any problem and have quires, please feel free to ask it from below comment section and you can ask it on SLACK.
But sometimes in the mobile applications, we have to put JSON file in the application package rather than get it from the server. In the mobile application, we can put it in anywhere in the application directory as per platform guideline. Such as in Android and Flutter, we can put a JSON file in the assets folder and parse it on runtime to get the data and use it accordingly.
In this post, we'll create a Flutter application and see how to parse a JSON locally within our app. The final output of app looks like below:
Let's dive into Flutter app development with the help of following steps.
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 country.json in the assets folder. To access this file in the Flutter application, we have to declare it in the pubspecs.yaml file. As we have did:
3. After that, create a list.dart file. In this file, we'll pass a list of countries array as a param and displaying it in the build method by using the listview.
in the builder param snapshot.data.toString() represent a string of JSON. We pass it to parseJson() method for parse it.
the parseJson() method make a list of countries.
Here, you can see the complete main.dart file. Now merge all above snippet and run the app.