Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to parsing JSON in Flutter

Tags:

flutter

dart

I am trying to get data from the internet in Flutter. But I am getting an error on JSON parsing. Can anyone tell me what is the problem?

I am trying to get data from this URL

https://swapi.co/api/starships/

Example JSON

{
    "count": 37, 
    "next": "https://swapi.co/api/starships/?page=2", 
    "previous": null, 
    "results": [
        {
            "name": "Executor", 
            "model": "Executor-class star dreadnought", 
            "manufacturer": "Kuat Drive Yards, Fondor Shipyards", 
            "cost_in_credits": "1143350000", 
            "length": "19000", 
            "max_atmosphering_speed": "n/a", 
            "crew": "279144", 
            "passengers": "38000", 
            "cargo_capacity": "250000000", 
            "consumables": "6 years", 
            "hyperdrive_rating": "2.0", 
            "MGLT": "40", 
            "starship_class": "Star dreadnought", 
            "pilots": [], 
            "films": [
                "https://swapi.co/api/films/2/", 
                "https://swapi.co/api/films/3/"
            ], 
            "created": "2014-12-15T12:31:42.547000Z", 
            "edited": "2017-04-19T10:56:06.685592Z", 
            "url": "https://swapi.co/api/starships/15/"
        }, 

    ]
}

Model class

class RestModel {
    final String name;
    final String model;
    final String manufacturer;
    final String cost_in_credits;
    final String length;
    final String max_atmosphering_speed;
    final String crew;
    final String passengers;
    final String cargo_capacity;
    final String consumables;
    final String hyperdrive_rating;
    final String MGLT;
    final String starship_class;
    final List films;
    final String pilots;
    final String created;
    final String edited;
    final String url;

    RestModel(
        {this.name,
        this.model,
        this.manufacturer,
        this.cost_in_credits,
        this.length,
        this.max_atmosphering_speed,
        this.crew,
        this.passengers,
        this.cargo_capacity,
        this.consumables,
        this.hyperdrive_rating,
        this.MGLT,
        this.starship_class,
        this.films,
        this.pilots,
        this.created,
        this.edited,
        this.url});

    factory RestModel.fromJson(Map<String, dynamic> json) {
        return RestModel(
            name: json["name"],
            model: json["model"],
            manufacturer: json["manufacturer"],
            cost_in_credits: json["cost_in_credits"],
            max_atmosphering_speed: json["max_atmosphering_speed"],
            crew: json["crew"],
            passengers: json["passengers"],
            cargo_capacity: json["cargo_capacity"],
            consumables: json["consumables"],
            hyperdrive_rating: json["hyperdrive_rating"],
            MGLT: json["MGLT"],
            starship_class: json["starship_class"],
            films: json["flims"],
            pilots: json["pilots"],
            created: json["created"],
            edited: json["edited"],
            url: json["url"],
        );
    }
}

and the Flutter code is:

final link = "https://swapi.co/api/starships/";

List<RestModel> list;

Future getData() async {
    var res = await http
    .get(Uri.encodeFull(link), headers: {"Accept":"application/json"});

    if (res.statusCode == 200) {
        var data = json.decode(res.body);
        var rest = data["results"];
        for (var model in rest) {
            list.add(RestModel.fromJson(model));
        }
        print("List Size: ${list.length}");
    }  
}

The main problem is when it tries to fill data from JSON.

RestModel.fromJson(model)

so what I have to change to fix this problem.

like image 970
Shudipto Trafder Avatar asked Oct 17 '22 16:10

Shudipto Trafder


1 Answers

Try to cast the data 'results' to List , like this :

var rest = data["results"] as List; 

Updated

Now that we know the error log: "No static method 'fromJson' declared in class 'RestModel'"

It's because you are using a static method in this line:

list.add(RestModel.fromJson(model));

You must change the call in order to use the factory constructor, like this :

list.add(new RestModel.fromJson(model));
like image 71
diegoveloper Avatar answered Oct 21 '22 06:10

diegoveloper