Home > Mobile >  Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<Stri
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<Stri

Time:01-06

I am trying to use my json data, which I received using http package. But I am getting this exception while storing a list of passengers from json file. I am not sure, due to which line, exception is occuring, therefore I am giving you the log details. I think, because of line number 54 has some issues or maybe line 25 has some in cinverting and storing it into array.

passenger.json -

import 'dart:convert';

PassengersData passengersDataFromJson(String str) => PassengersData.fromJson(json.decode(str));

String passengersDataToJson(PassengersData data) => json.encode(data.toJson());

class PassengersData {
  PassengersData({
    required this.totalPassengers,
    required this.totalPages,
    required this.data,
  });

  int totalPassengers;
  int totalPages;
  List<Passenger> data;

  factory PassengersData.fromJson(Map<String, dynamic> json) => PassengersData(
    totalPassengers: json["totalPassengers"],
    totalPages: json["totalPages"],
    data: List<Passenger>.from(json["data"].map((x) => Passenger.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "totalPassengers": totalPassengers,
    "totalPages": totalPages,
    "data": List<dynamic>.from(data.map((x) => x.toJson())),
  };
}

class Passenger {
  Passenger({
    required this.id,
    required this.name,
    required this.trips,
    required this.airline,
    required this.v,
  });

  String id;
  String name;
  int trips;
  Airline airline;
  int v;

  factory Passenger.fromJson(Map<String, dynamic> json) => Passenger(
    id: json["_id"],
    name: json["name"],
    trips: json["trips"],
    airline: Airline.fromJson(json["airline"]),
    v: json["__v"],
  );

  Map<String, dynamic> toJson() => {
    "_id": id,
    "name": name,
    "trips": trips,
    "airline": airline.toJson(),
    "__v": v,
  };
}

class Airline {
  Airline({
    required this.id,
    required this.name,
    required this.country,
    required this.logo,
    required this.slogan,
    required this.headQuaters,
    required this.website,
    required this.established,
  });

  int id;
  String name;
  String country;
  String logo;
  String slogan;
  String headQuaters;
  String website;
  String established;

  factory Airline.fromJson(Map<String, dynamic> json) => Airline(
    id: json["id"],
    name: json["name"],
    country: json["country"],
    logo: json["logo"],
    slogan: json["slogan"],
    headQuaters: json["head_quaters"],
    website: json["website"],
    established: json["established"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "name": name,
    "country": country,
    "logo": logo,
    "slogan": slogan,
    "head_quaters": headQuaters,
    "website": website,
    "established": established,
  };
}

Exception is occuring here -

E/flutter ( 8778): #0      new Passenger.fromJson (package:deeplinking/model/passenger_data.dart:54:35)
E/flutter ( 8778): #1      new PassengersData.fromJson.<anonymous closure> (package:deeplinking/model/passenger_data.dart:25:66)
E/flutter ( 8778): #2      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
E/flutter ( 8778): #3      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
E/flutter ( 8778): #4      new List.from (dart:core-patch/array_patch.dart:41:17)
E/flutter ( 8778): #5      new PassengersData.fromJson (package:deeplinking/model/passenger_data.dart:25:11)
E/flutter ( 8778): #6      passengersDataFromJson (package:deeplinking/model/passenger_data.dart:7:69)
E/flutter ( 8778): #7      _HomePageState.getPassengerData (package:deeplinking/main.dart:131:22)
E/flutter ( 8778): <asynchronous suspension>
E/flutter ( 8778): #8      _HomePageState.build.<anonymous closure> (package:deeplinking/main.dart:161:26)
E/flutter ( 8778): <asynchronous suspension>

What should I do to parse it?

Here is the json data -

{
   "totalPassengers":19673,
   "totalPages":1968,
   "data":[
      {
         "_id":"5ff393986feae02b6c22b251",
         "name":"Deepanss",
         "trips":0,
         "airline":[
            {
               "id":3,
               "name":"Cathay Pacific",
               "country":"Hong Kong",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/1/17/Cathay_Pacific_logo.svg/300px-Cathay_Pacific_logo.svg.png",
               "slogan":"Move Beyond",
               "head_quaters":"Cathay City, Hong Kong International Airport, Chek Lap Kok, Hong Kong",
               "website":"www.cathaypacific.com",
               "established":"1946"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff3939c6feae085f522b252",
         "name":"Deepan Chakravarthy",
         "trips":250,
         "airline":[
            {
               "id":5,
               "name":"Eva Air",
               "country":"Taiwan",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/e/ed/EVA_Air_logo.svg/250px-EVA_Air_logo.svg.png",
               "slogan":"Sharing the World, Flying Together",
               "head_quaters":"376, Hsin-Nan Rd., Sec. 1, Luzhu, Taoyuan City, Taiwan",
               "website":"www.evaair.com",
               "established":"1989"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff3939c6feae03a4f22b253",
         "name":null,
         "trips":null,
         "airline":[
            {
               "id":8,
               "name":"Thai Airways",
               "country":"Thailand",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/5/58/Thai_Airways_Logo.svg/200px-Thai_Airways_Logo.svg.png",
               "slogan":"Smooth as Silk / I Fly THAI",
               "head_quaters":"Jom Phol Subdistrict, Chatuchak, Bangkok, Thailand",
               "website":"www.thaiairways.com",
               "established":"1960"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff393d76feae02aff22b254",
         "name":"Percy Townsend",
         "trips":250,
         "airline":[
            {
               "id":5,
               "name":"Eva Air",
               "country":"Taiwan",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/e/ed/EVA_Air_logo.svg/250px-EVA_Air_logo.svg.png",
               "slogan":"Sharing the World, Flying Together",
               "head_quaters":"376, Hsin-Nan Rd., Sec. 1, Luzhu, Taoyuan City, Taiwan",
               "website":"www.evaair.com",
               "established":"1989"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff393de6feae0033322b256",
         "name":"Percy Townsend",
         "trips":250,
         "airline":[
            {
               "id":5,
               "name":"Eva Air",
               "country":"Taiwan",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/e/ed/EVA_Air_logo.svg/250px-EVA_Air_logo.svg.png",
               "slogan":"Sharing the World, Flying Together",
               "head_quaters":"376, Hsin-Nan Rd., Sec. 1, Luzhu, Taoyuan City, Taiwan",
               "website":"www.evaair.com",
               "established":"1989"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff393de6feae0422c22b257",
         "name":null,
         "trips":null,
         "airline":[
            {
               "id":8,
               "name":"Thai Airways",
               "country":"Thailand",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/5/58/Thai_Airways_Logo.svg/200px-Thai_Airways_Logo.svg.png",
               "slogan":"Smooth as Silk / I Fly THAI",
               "head_quaters":"Jom Phol Subdistrict, Chatuchak, Bangkok, Thailand",
               "website":"www.thaiairways.com",
               "established":"1960"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff393f16feae0183f22b25a",
         "name":"Percy Townsend",
         "trips":250,
         "airline":[
            {
               "id":5,
               "name":"Eva Air",
               "country":"Taiwan",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/e/ed/EVA_Air_logo.svg/250px-EVA_Air_logo.svg.png",
               "slogan":"Sharing the World, Flying Together",
               "head_quaters":"376, Hsin-Nan Rd., Sec. 1, Luzhu, Taoyuan City, Taiwan",
               "website":"www.evaair.com",
               "established":"1989"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff394296feae0e66222b261",
         "name":null,
         "trips":null,
         "airline":[
            {
               "id":8,
               "name":"Thai Airways",
               "country":"Thailand",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/5/58/Thai_Airways_Logo.svg/200px-Thai_Airways_Logo.svg.png",
               "slogan":"Smooth as Silk / I Fly THAI",
               "head_quaters":"Jom Phol Subdistrict, Chatuchak, Bangkok, Thailand",
               "website":"www.thaiairways.com",
               "established":"1960"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff394526feae05df122b262",
         "name":"Percy Townsendss",
         "trips":0,
         "airline":[
            {
               "id":10,
               "name":"Swiss International Air Lines",
               "country":"Switzerland",
               "logo":"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Swiss_International_Air_Lines_Logo_2011.svg/200px-Swiss_International_Air_Lines_Logo_2011.svg.png",
               "slogan":"Made of Switzerland. The Airline of Switzerland",
               "head_quaters":"EuroAirport Basel Mulhouse Freiburg near Basel, Switzerland",
               "website":"www.swiss.com",
               "established":"2002"
            }
         ],
         "__v":0
      },
      {
         "_id":"5ff394536feae0839d22b263",
         "name":"Bruno Wilcher",
         "trips":300,
         "airline":[
            {
               "id":5,
               "name":"Eva Air",
               "country":"Taiwan",
               "logo":"https://upload.wikimedia.org/wikipedia/en/thumb/e/ed/EVA_Air_logo.svg/250px-EVA_Air_logo.svg.png",
               "slogan":"Sharing the World, Flying Together",
               "head_quaters":"376, Hsin-Nan Rd., Sec. 1, Luzhu, Taoyuan City, Taiwan",
               "website":"www.evaair.com",
               "established":"1989"
            }
         ],
         "__v":0
      }
   ]
}

I tried using airline: Airline.fromJson(json["airline"][0]),, but then it is giving me error- Unhandled Exception: type 'Null' is not a subtype of type 'String'.

You can visit this link - json data

CodePudding user response:

I have not tried this yet but i think the problem is that you are trying to convert an object of Airlines instead of a List.

Try to add this: List<Airline>.from(json["airline"].map((x) => Airline.fromJson(x)))

in Passenger's Factory method:

factory Passenger.fromJson(Map<String, dynamic> json) => Passenger(
  id: json["_id"],
  name: json["name"],
  trips: json["trips"],
  airline: List<Airline>.from(json["airline"].map((x) => Airline.fromJson(x))),
  v: json["__v"],);

CodePudding user response:

The first error you get comes from json['airline'] being a list with only one item, it gets fixed by doing json['airline'][0]. But then a new error occurs, you are passing some null value to a string. This new error has absolutely nothing to do with the previous error and is related instead to your data.

On your data, both the name and the number of trips are nullable. You will see this if you Ctrl F null on your data. So in order to accommodate for this, your model should make those fields nullable as well.

class Passenger {
  Passenger({
    required this.id,
    required this.name,
    required this.trips,
    required this.airline,
    required this.v,
  });

  String id;
  String? name; // here
  int? trips;   // and here
  Airline airline;
  int v;

  factory Passenger.fromJson(Map<String, dynamic> json) => Passenger(
    id: json["_id"],
    name: json["name"],
    trips: json["trips"],
    airline: Airline.fromJson(json["airline"][0]),
    v: json["__v"],
  );

  Map<String, dynamic> toJson() => {
    "_id": id,
    "name": name,
    "trips": trips,
    "airline": airline.toJson(),
    "__v": v,
  };
}

CodePudding user response:

you have a problem in this line airline: Airline.fromJson(json["airline"]) because json["airline"] is a List and you are passing it as a Map<String, dynamic>

To solve the problem try this:

  1. in Passenger model: change this line Airline airline; to List airlines;
  2. replace Passenger.fromJson to this:

2- replace Passenger.fromJson to this:

factory Passenger.fromJson(Map<String, dynamic> json) => Passenger(

id: json["_id"],
name: json["name"],
trips: json["trips"],
airline: List<Airline>.from(json["airline"].map((x) => Passenger.fromJson(x))),
v: json["__v"],

);

  •  Tags:  
  • Related