1

I am trying to parse JSON data in Flutter and it is working fine. But when some data is empty or null then it is not working and giving error.

import 'dart:convert';

GetFollowing getFollowingFromJson(String str) => GetFollowing.fromJson(json.decode(str));

class GetFollowing {
    List<Content> content;
    int count;
    bool success;
    String error;

    GetFollowing({
        this.error,
        this.content,
        this.count,
        this.success,
    });

    factory GetFollowing.fromJson(Map<String, dynamic> json) => GetFollowing(
        error: json["error"],
        content: List<Content>.from(json["content"].map((x) => Content.fromJson(x))),
        count: json["count"],
        success: json["success"],
    );

}

class Content {
    int uid;
    int following;
    String name;
    String profilePhoto;
    String baseLocation;
    String registrationDate;
    String country;

    Content({
        this.uid,
        this.following,
        this.name,
        this.profilePhoto,
        this.baseLocation,
        this.registrationDate,
        this.country,
    });

    factory Content.fromJson(Map<String, dynamic> json) => Content(
        uid: json["uid"] ?? null,
        following: json["following"] ?? null,
        name: json["name"] ?? null,
        profilePhoto: json["profile_photo"] ?? null,
        baseLocation: json["base_location"] ?? null,
        registrationDate: json["registration_date"] ?? null,
        country: json["country"] ?? null,
    );

}

Here is the sample data.

  1. Scenario

    {"error":"No record found.","success":false}

  2. Scenario

{
    "content": [{
        "uid": 34,
        "following": 35,
        "name": "Sadi",
        "profile_photo": "noimage.png",
        "base_location": "New Delhi",
        "registration_date": "03-05-2020",
        "country": "India"
    }, {
        "uid": 34,
        "following": 37,
        "name": "Sameer",
        "profile_photo": "noimage.png",
        "base_location": "New Delhi",
        "registration_date": "03-05-2020",
        "country": "India"
    }, {
        "uid": 34,
        "following": 36,
        "name": "Simran",
        "profile_photo": "noimage.png",
        "base_location": "New Delhi",
        "registration_date": "03-05-2020",
        "country": "India"
    }],
    "count": 3,
    "success": true
}

I am getting error if content is null or empty then parse didn't complete and gives error.

Exception Caught: NoSuchMethodError: The method '[]' was called on null.

1 Answer 1

1
content: (json["content"] as List).map((x as Map<String, dynamic>) => Content.fromJson(x)).toList()

Consider json_annotation or built_value to handle all your boiler plate for you.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. Where do i need to place it exactly?
I placed your code and it is now giving different syntax error ---- The method 'toList' isn't defined for the type 'GetFollowing'. Try correcting the name to the name of an existing method, or defining a method named 'toList'.
It is still giving error. Tried calling: map<Content>(Closure: (dynamic) => Content)
i tried updated answer but it is giving syntax error on x and Map. Undefined class 'x'. Try changing the name to the name of an existing class, or creating a class with the name 'x'. Map ------ Expected to find ')'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.