How to Convert Response From JSON to Object In Flutter ?

· 3 min read
How to Convert Response From JSON to Object In Flutter
How to Convert Response From JSON to Object In Flutter

We need to convert JSON to Object-based on format Response Received from Backend.  So in this article, we will go through How to convert Response From JSON to Object In Flutter?

How to Convert Response From JSON to Object In Flutter?

Depending on the type of response we received from the backend we need to convert it into an appropriate format so that it can be readable by the mobile user. Let’s Consider that we will get JSON like below:

[
   {
     "id":1,
     "name":"Afghanistan",
     "iso3":"AFG",
     "iso2":"AF",
     "phone_code":"93",
     "capital":"Kabul",
     "currency":"AFN"
   },
   {
     "id":2,
     "name":"Aland Islands",
     "iso3":"ALA",
     "iso2":"AX",
     "phone_code":"+358-18",
     "capital":"Mariehamn",
     "currency":"EUR"
     },
  ]

POJO Class will look like below:

class country{
    String id;
    String name;
    String currency;

    country({this.id,this.name,this.currency});

    factory country.fromJson(Map<String, dynamic> parsedJson){
      return country(
      id: parsedJson['id'].toString(),
      name : parsedJson['name'].toString(),
      currency: parsedJson['currency'].toString() 
      );
    }
 }

API CALL :

await http.post("http://calikidsmap.com/test.php").then((response){

   var ddd=jsonDecode(response.body);

   Country_object_list = (ddd as List)
     .map((data) => new country.fromJson(data))
     .toList();

You can parse directly to a Map

final Map parsed = json.decode(res);

After you have a map you can use that data to convert into your Object.

final signUp = SignUpResponse.fromJson(parsed);

And if you want to parse an array of objects, you could do something like this:

//assuming this json returns an array of signupresponse objects
final List parsedList = json.decode(res); 

List<SignUpResponse> list = parsedList.map((val) =>  SignUpResponse.fromJson(val)).toList();

Conclusion:

In this article, we have been through How to Convert Response From JSON to Object In Flutter?

Hope you like our articles. Do Share Your Suggestions/Feedback to Serve you much better.

Still, Need a Support for Flutter Development? We would love you to assist you.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

3 comments

  1. final response =
    await http.post(“url”);

    final List extracteddata=jsonDecode(response.body);

    List lst=extracteddata.map((val) => UserParty.fromJson(val)).toList();

    this is my model class
    factory UserParty.fromJson(Map json) {

    return UserParty(

    id: int.parse(json[“id”]),
    partyName: json[“strPartyName”].toString(),
    partyimage: json[“StrPartyImage”].toString(),

    candidateName: json[“strPPersonName”].toString(),
    description: “The Person is Participating as ${json[‘strPosition’]}”,

    color: Color(int.parse(json[“color”])),

    candidateimage: json[“strPPersonImage”].toString(),

    position: json[“strPosition”].toString(),

    );
    }
    iwas getting the following error when convert to dart objecttype ‘String’ is not a subtype of type ‘List

    not able to convert into dart object .struggle on this for more than 3 days.
    please help me on this

  2. Dear veeranjaneyulu ,

    Hope this message finds you a well !!!

    Thanks for reading the article and your response is noted.

    If we have got you right you are getting error on ” not able to convert into dart object .struggle on this for more than 3 days. ” when convert to dart objecttype ‘string’ is not a subtype of type list, RIght ??

    Awaiting for your prompt response.

    Warm Regards,
    Flutter Agency

Leave a Reply