Home > Net >  Sending HTTP Request from flutter getting Error type 'List<dynamic>' is not a subtyp
Sending HTTP Request from flutter getting Error type 'List<dynamic>' is not a subtyp

Time:01-05

Im trying to get the data using query parameters in the uri

uri = uri.replace(queryParameters: {'with': 'nutrition;restaurant;category;extras;extraGroups;options;optionGroups;foodReviews;foodReviews.user'})

However when I remove both options and optionGroups from the uri it works fine with no error. There is nothing different between how they are stored in the database or their api controllers.

I know its definitely an oversight on my part but I cant recall and need some ideas as to what I missed or did wrong that would cause that error.

Full code below:

Future<Stream<Food>> getFood(String foodId) async {

  Uri uri = Helper.getUri('api/foods/$foodId');

  uri = uri.replace(queryParameters: {'with': 'nutrition;restaurant;category;extras;extraGroups;foodReviews;foodReviews.user'});
  try {

    final client = new http.Client();
    final streamedRest = await client.send(http.Request('get', uri));
    log( streamedRest.stream.toString()  );
    return streamedRest.stream.transform(utf8.decoder).transform(json.decoder).map((data) => Helper.getData(data)).map((data) {
      return Food.fromJSON(data);

    });

  } catch (e) {
    print(CustomTrace(StackTrace.current, message: uri.toString()).toString());
    return new Stream.value(new Food.fromJSON({}));
  }
}

This is the model:

    import '../helpers/custom_trace.dart';

class OptionGroup {
  String id;
  String name;
  
 OptionGroup();

 OptionGroup.fromJSON(Map<String, dynamic> jsonMap) {
    try {
      id = jsonMap['id'].toString();
      name = jsonMap['name'];

    } catch (e) {
      id = '';
      name = '';
      print(CustomTrace(StackTrace.current, message: e));
    }
  }

  Map toMap() {
    var map = new Map<String, dynamic>();
    map["id"] = id;
    map["name"] = name;

    return map;
  }

  @override
  String toString() {
    return this.toMap().toString();
  }

  @override
  bool operator ==(dynamic other) {
    return other.id == this.id;
  }

  @override
  int get hashCode => this.id.hashCode;
}

CodePudding user response:

When we recieve or send data, it should be in JSON format which equivalent to Map in dart. So make sure the data should be in Map while sending the request. You can do so by object.toMap().

CodePudding user response:

As I can see you are passing the complete object in the query. what you need to do is send the object data in a map format using toMap() function of the class

  •  Tags:  
  • Related