1
votes

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

1 Answers

1
votes

For anyone who hits this issue, I did the following...

Future<List<Attendee>> callLogin() async {
return http.get(
    "http://www.somesite.com")
.then((response) {
  try {
    List<Attendee> l = new List();

//  final Map<String, dynamic> responseJson = // REPLACED 
    final dynamic responseJson =              // <<== REMOVED CAST to Map<String, dynamic>
                                              // 
        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
  }
});

Normally I don't attempt to answer my own questions. And, in this case I still do not have a true answer why Flutter acts differently than running the code from the Dart main. I am running them both from Android Studio. So, I would assume that Dart, in both cases would be the same version. But, that is where I am looking next. I will post if I have more information.