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.
List<String> isn't type related to List<Object> in any way, not anymore than List<String> is related to List<Number> . The type is List<String> , the entire signature is the type . But like with everything, there are exceptions; these are called Wildcard Parameterized Types .
In dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.
json.encode(data)
in bodyCreate 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());
}
I found the answer. I just added .toString()
to the _filters
List.
this.api.setInterests(token, _filters.toString())
.then((res) {
print(res);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With