Parsing large JSON files can lead to poor performance of apps and shuttering animations. If parsing and computing a large JSON file take more than 16 milliseconds, the users will experience Jank. Dart by default uses a single thread to perform these tasks, though being simple and fast for less expensive computation, it fails when the computation in large.
To avoid these janks, isolates can be used to perform all computations in a different thread in the background. In this article, we will explore the process of parsing JSON in the background. To do so follow the below steps:
- Import the http package.
- Make a network request using the http package
- Change the response into dart objects in list form
- Move this work to a different isolate
Importing The http Package:
To install the http package use the below command in your command prompt:
or, if you’re using the flutter cmd use the below command:
flutter pub get
After the installation add the dependency to the pubsec.yml file as shown below:
import 'package:http/http.dart' as http;
We can use the http.get() method to fetch the sample data of 5000 images from JSONPlaceholder REST API as shown below:
Parsing and Converting the Data:
We now have 5000 photos in JSON form received from the http.response and these need to be parsed and converted into a list of Dart objects. To do show First create a Photo class as shown below.
Here we will create a Photo class that contains the JSON data as shown below:
Now update the getPhoto() function to returns a Future<List<Photo>> through the below steps:
- Make a Photos_parser() function to convert the response body into a List<Photo>.
- Use the Photos_parser() function in the getPhotos() function.
Moving work to a different Isolate:
The compute() function can be used to move the work to a separate isolate where it will be parsed and converted in the background. It runs expensive functions in a background isolate and returns the result.
Complete Source Code:
- Getting Started with Cross-Platform Mobile Application using Flutter
- MaterialApp class in Flutter
- Difference between React Native and Flutter
- Scaffold class in Flutter with Examples
- Difference Between Rows and Columns vs Container in Flutter
- Multi Page Applications in Flutter
- Routes and Navigator in Flutter
- Flutter GridView
- What is widgets in Flutter?
- Container class in Flutter
- Difference Between Stateless and Stateful Widget in Flutter
- Rive animations in Flutter
- ProgressIndicator in Flutter
- Row and Column Widgets in Flutter with Example
- Flutter - Stateful vs Stateless Widgets
- Wrap Widget in Flutter
- Listview.builder in Flutter
- Splash Screen in Flutter
- How to Add images in Flutter App?
- A Hello World App using Flutter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.