Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  Future setInterests(String token, List<String> interests) {
    return _netUtil.post(BASE_URL + "/setinterests", body: {
      "token": token,
      "interests": interests
    }).then((dynamic res) {
      return res;

Passing _filters always throws the error:

type 'List<String>' is not a subtype of type 'String' in type cast

I don't know what else dart wants from me.

Check whether token is of type string too. Also try to define _filters with var instead of List<String> and remove <String>. That should be enough for most purposes in Dart. – Bostrot Jun 29, 2018 at 22:06 Checked. token is a string. It comes from final token = prefs.getString('token');. Changing to var doesn't fix it. The thing is, the error message makes absolutely no sense, so debugging is vague to even begin with – KhoPhi Jun 30, 2018 at 0:45 Using var throws this back type 'List<dynamic>' is not a subtype of type 'String' in type cast. – KhoPhi Jun 30, 2018 at 0:48 Are you sure that body can take in List's? It looks like it is trying to cast it to a String to me, which obviously does not work. – creativecreatorormaybenot Jun 30, 2018 at 7:39

I found the answer. I just added .toString() to the _filters List.

  this.api.setInterests(token, _filters.toString())
  .then((res) {
    print(res);
                Good to hear that you solved your issue. I was just writing a comment as a friendly reminder. :)  First of all,  List<String> _filters = <String>[8, 11]; this part is giving an error.  This will give us a error below. ` error: The element type 'int' can't be assigned to the list type 'String'. ` I would fix the array the array issue too.
– salihgueler
                Jun 30, 2018 at 11:42
  • You need to add json.encode(data) in body
  • Add these two header
    'Content-type': 'application/json',
    'Accept': 'application/json',
  • Create map of your data

    final Map<String, dynamic> data = new Map<String, dynamic>();
     data['token'] = token;
     data['interests'] = interests;
    
  • Call api like this

    http.post(url,<br>
    body: json.encode(data),
    headers: { 'Content-type': 'application/json',
      'Accept': 'application/json'},
    encoding: encoding)
    .then((http.Response response) {
         // print(response.toString());
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •