In this new flutter tutorial, you will learn how to download data in flutter, how to use the future builder flutter functionality to download the data asynchronously, how to parse the json data in a dart structure by decoding it using the compute flutter functionality to perform the operation on an external thread. In this simple flutter example you will learn the basic of the http flutter library and of json parsing operation by doing it in the background. You will see ho to parse json list, json string and more complex object.
Open Android Studio and start a new Flutter Application Project.
Add now the http library by open the pubspec.yaml file and add the http library dependency. Press now on the Get Package button to download the http library.
The future builder flutter functionality allows you to perform an async task. The future builder will notify as soon as the data is ready, or if an error while performing the async task happened. Here of the structure of the future builder looks like:
The fetchFunction is the async function called by the future builder. The snapshot variable contains the data as soon as is ready and the error if happened.
The fetchFunction in this example will perform the http data request. Then, as soon the data has been received with the 200 http status code, the json parsing operation will be performed on an external thread using the compute functionality.
To perform the json parsing operation we will use the fromJson utility.
We have then to define our structure and the type of the data. Here is the json data example used: json data example. We have different cases to handle, a string, an integer, an array of strings, an array of objects and a simple object.
Let's define our data structure. In particular we have to define the variable names and types that will be filled by the json structure, and how the parsing operation will be performed. Let's start by the string. We define the string variable, and the fromJson function
Let's define now the list of strings.
Define now the array of objects definition and the single object.
Here you can download the project example: