PoojaB26/ParsingJSON-Flutter

type 'List<dynamic>' is not a subtype of type 'String'

my9074 opened this issue · 4 comments

void main() {
  loadPhotos(); // has Unhandled Exception
  runApp(new MyApp());

Report this error:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'String'
#0      loadPhotos (package:flutter_json/services/photo_services.dart:13:21)
<asynchronous suspension>                                               
#1      main (package:flutter_json/main.dart:12:3)                      
#2      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:189:25)
#3      _rootRun (dart:async/zone.dart:1124:13)                         
#4      _CustomZone.run (dart:async/zone.dart:1021:19)                  
#5      _runZoned (dart:async/zone.dart:1516:10)                        
#6      runZoned (dart:async/zone.dart:1500:12)                         
#7      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:180:5)    
#8      _startIsolate.<anonymous closure> (dart:isolate/runtime/lib/isolate_patch.dart:300:19)
#9      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/lib/isolate_patch.dart:171:12)

Where do you see this error? I don't get this.

import 'dart:convert';

class CountryList {
final List photos;

CountryList({
this.photos,
});

factory CountryList.fromJson(List parsedJson) {

List<CountryResponse> photos = new List<CountryResponse>();
photos = parsedJson.map((i)=>CountryResponse.fromJson(i)).toList();

return new CountryList(
    photos: photos
);

}
}

class CountryResponse {
int countryId;
String countryName;

CountryResponse({
this.countryId,
this.countryName,
});

factory CountryResponse.fromJson(Map<String, dynamic> json) => CountryResponse(
countryId: json["CountryId"],
countryName: json["CountryName"],
);

Map<String, dynamic> toJson() => {
"CountryId": countryId,
"CountryName": countryName,
};
}

`Future<List> getCountry() async {
print("hey");
String finalUrl = postsURL+'Country/GetCountry?key=$key';
final response = await http.get(finalUrl);
if(response.statusCode == 200) {
var data = json.decode(response.body);
print(data.toString());
var rest = data as List;
return CountryList.fromJson(rest).photos;
}
}

Hope this code snippet helped you.

json response

[
{
"CountryId": 13,
"CountryName": "Australia"
},
{
"CountryId": 230,
"CountryName": "United Kingdom"
}
]