nassqra
nassqra

Reputation: 403

Flutter/Dart: Type 'int' is not a subtype of type 'String' in type cast

I have gone through answers to this question, yet none of them cover the case I seem to have (I maybe wrong due to lack of comprehending the answers). Hence, I ought to ask.

I have tested the API with Postman with the same parameters and data, and it works just fine.

onPressed: ()async {
                        print('Counter: $counter, Type: $type, Phone: $phone, Token: $token');
                        print(counter.runtimeType);
                        if(counter != 0){
                          print('above post now');
                          var response = await http.post(
                            acceptPass, //final URL in string

                            body:{
                              'type': type, //string
                              'phone': phone, //string
                              'qty': counter //int. Tried sending as string too, didn't work.
                            },
                            headers: {
                              'token': token //string
                            }
                          );
                          print("something with the response here ");
                          print(response.statusCode);
                        }

                      },

None of the values are null, I did print them out and check them before putting them into the post method. I also tried typecasting each of the variables to string and then Posting it, but to no avail.

enter image description here

Upvotes: 0

Views: 1723

Answers (1)

Richard Heap
Richard Heap

Reputation: 51750

body needs to be a Map<String, String>. So, you need to convert the int to a string before putting it into the map.

Add .toString() after counter.

To have the compiler assist you with type checking, you can explicitly type the map as follows.

  body: <String, String>{
    'fieldName': 'fieldValue',
    //etc

Upvotes: 1

Related Questions