I want to convert a string into class object. The example string is Like this :
[{
"MESSAGE": "Successfully!",
"ORIGINAL_ERROR": "",
"ERROR_STATUS": false,
"RECORDS": true,
"Data": [{
"Id": "1",
"Name": "Third AC"
}, {
"Id": "2",
"Name": "Second AC"
}, {
"Id": "3",
"Name": "First AC"
}, {
"Id": "4",
"Name": "AC Chair Car"
}, {
"Id": "5",
"Name": "First Class"
}, {
"Id": "6",
"Name": "Sleeper Class"
}, {
"Id": "7",
"Name": "Second Class Unreserved"
}, {
"Id": "8",
"Name": "3AC Economy "
}, {
"Id": "9",
"Name": "Second Seating"
}, {
"Id": "10",
"Name": "Executive Class"
}]
}]
And I Want to convert the string to the class object. the Class Is
class TrainClassData {
String MESSAGE;
String ORIGINAL_ERROR;
String ERROR_STATUS;
String RECORDS;
List<TrainClass> Data;
TrainClassData({
this.MESSAGE,
this.ORIGINAL_ERROR,
this.ERROR_STATUS,
this.RECORDS,
this.Data,
});
factory TrainClassData.fromJson(Map<String, dynamic> json) {
return TrainClassData(
MESSAGE: json['Id'] as String,
ORIGINAL_ERROR: json['Name'] as String,
ERROR_STATUS: json['Name'] as String,
RECORDS: json['Name'] as String,
Data: json['Data'].map<TrainClass>((json) => TrainClass.fromJson(json)).toList()
);
}
}
class TrainClass {
String Id;
String Name;
TrainClass({
this.Id,
this.Name,
});
factory TrainClass.fromJson(Map<String, dynamic> json) {
return TrainClass(
Id: json['Id'] as String, Name: json['Name'] as String);
}
}
I am using a service, but when ever i am trying to use this class to get data it gives me error. My Service Call Is Like This.
static Future<List<TrainClassData>> GetTrainClass() async {
String url = 'http://ttreturn.itfuturz.com/AppService.asmx/TrainClassMaster?type=trainclass';
print("train class URL: " + url);
final response = await http.get(url);
try {
if (response.statusCode == 200) {
List<TrainClassData> list = [];
if (response.body != "" && response.body.toString() != "[]") {
final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
var trainClassData = parsed.map<TrainClassData>((json) => TrainClassData.fromJson(json));
if(trainClassData.ERROR_STATUS == false) {
list = trainClassData.Data;
}
else {
list = [];
}
}
return list;
} else {
throw Exception(MESSAGES.INTERNET_ERROR);
}
} catch (e) {
print("Kapil Erorr : " + e.toString());
throw Exception(MESSAGES.INTERNET_ERROR);
}
}
I am getting error.
NoSuchMethodError: Class 'MappedListIterable, TrainClassData>' has no instance getter 'ERROR_STATUS'.
Please Help Me. Thanks in advance.