How to Deserialize a list of objects from json in flutter Ask Question

0 votes

I am using the dart package json_serializable for json serialization. Looking at the flutter documentation it shows how to deserialize a single object as follow:

Future<Post> fetchPost() async {
  final response =
  await http.get('https://jsonplaceholder.typicode.com/posts/1');

  if (response.statusCode == 200) {
  // If the call to the server was successful, parse the JSON
  return Post.fromJson(json.decode(response.body));
  } else {
    // If that call was not successful, throw an error.
    throw Exception('Failed to load post');
  }
}

However, I am not familiar enough with dart to figure out how to do the same for a list of items instead of a single instance.

Mar 28, 2023 in Flutter by Ashwini
• 5,430 points
2,282 views

1 answer to this question.

0 votes

To deserialize a list of objects from JSON in Flutter:

  1. Create a model class for your object and annotate it with @JsonSerializable().
  2. Add the necessary dependencies to your pubspec.yaml file.
  3. Generate the JSON serialization code by running flutter pub run build_runner build.
  4. Use http package to fetch the JSON data, and then use json.decode() and List.map() to convert the list of maps to a list of objects.

To know more about Flutter, join our Flutter Certification today.

answered Mar 28, 2023 by vishalini

Related Questions In Flutter

0 votes
1 answer

How to implement drop down list in flutter?

You're close! The error you're getting is ...READ MORE

answered Mar 26, 2023 in Flutter by vishalini
4,086 views
0 votes
1 answer

How to convert json data into array in Flutter?

To convert the JSON data into an ...READ MORE

answered Mar 31, 2023 in Flutter by seena
12,641 views
0 votes
1 answer

How to get barcode from PDA scanner device in flutter

It's possible that the data sent by ...READ MORE

answered Apr 3, 2023 in Flutter by seena
1,944 views
0 votes
1 answer

Can't able fetch data form json using class in flutter(dart)

To fix the issue of null temperature ...READ MORE

answered Mar 18, 2023 in Android by vinayak
1,353 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,556 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,110 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,117 views
0 votes
2 answers

How to create a circle icon button in Flutter?

To create something similar to a FloatingActionButton, ...READ MORE

answered Aug 23, 2023 in Flutter by anonymous
• 140 points
14,356 views
0 votes
1 answer

How to find TableRow in a flutter test?

Based on the code you provided, it ...READ MORE

answered Apr 4, 2023 in Flutter by pooja
934 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP