9

I have this:

List<String> _filters = <String>[8, 11];

I pass this _filters into this endpoint:

this.api.setInterests(token, _filters)
  .then((res) {
    print(res);
  });

which looks like this:

  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.

5
  • 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. Commented Jun 29, 2018 at 22:06
  • 1
    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 Commented Jun 30, 2018 at 0:45
  • Using var throws this back type 'List<dynamic>' is not a subtype of type 'String' in type cast. Commented 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. Commented Jun 30, 2018 at 7:39
  • @creativecreatorormaybenot If so, how do I fix it? Commented Jun 30, 2018 at 10:40

2 Answers 2

5

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

  this.api.setInterests(token, _filters.toString())
  .then((res) {
    print(res);
  });
Sign up to request clarification or add additional context in comments.

1 Comment

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.
3
  1. You need to add json.encode(data) in body
  2. Add these two header
    'Content-type': 'application/json',
    'Accept': 'application/json',
  3. Create map of your data

    final Map<String, dynamic> data = new Map<String, dynamic>();
     data['token'] = token;
     data['interests'] = interests;
    
  4. 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());
    
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.