When I run this in a "Dart" main, everything works, and I get a List of attendees. However, when I call it within my Flutter application, I get the error:
flutter: type 'List' is not a subtype of type 'List>'
I have been round and round with the "not a subtype" error in Dart, and I finally was able to get all of the casts correct for my json data that is coming from a REST interface.
Can someone tell me, why is it bombing in Flutter but not in Dart? How do I resolve this?
Future<List<Attendee>> callLogin() async {
return http.get(
"http://www.somesite.com")
.then((response) {
try {
List<Attendee> l = new List();
final List<Map<String, dynamic>> responseJson =
json.decode(response.body)['attendees'];
responseJson.forEach((f) => l.add(new Attendee.fromJson(f)));
return l;
} catch (e) {
print(e); // Just print the error, for SO
}
});
}
class Attendee {
String nameFirst;
String nameLast;
String company;
...
factory Attendee.fromJson(Map<String, dynamic> json) {
return new Attendee(
nameLast: json['nameLast'],
nameFirst: json['tnameFirst'],
company: json['company'],
city: json['city'],
state: json['state'],
country: json['country'],
);
}
class AttendeeSearch extends StatefulWidget {
AttendeeSearch({Key key, this.title}) : super(key: key);
final String title;
@override
_AttendeeSearchPageState createState() => new
_AttendeeSearchPageState();
}
class _AttendeeSearchPageState extends State<AttendeeSearch> {
String search;
final _suggestions = new List<Attendee>();
final _smallerFont = const TextStyle(fontSize: 12.0);
final formKey = new GlobalKey<FormState>();
void _submit() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
}
AttendeeSummaryRequest asr = new AttendeeSummaryRequest();
setState(() {
asr.callLogin().then((item) { // Calling WebService
setState(() {
_suggestions.addAll(item);
});
});
});
}