Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
my9074 opened this issue Mar 2, 2019 · 4 comments
Open

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

my9074 opened this issue Mar 2, 2019 · 4 comments

Comments

@my9074
Copy link

my9074 commented Mar 2, 2019

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)
@PoojaB26
Copy link
Owner

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

@Android-Dheeraj
Copy link

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;
}
}

@Android-Dheeraj
Copy link

Hope this code snippet helped you.

@Android-Dheeraj
Copy link

json response

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants