Sushant Kumar
Sushant Kumar

Reputation: 509

Flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast

I'm sending a post api request from my flutter App to update the data of user and getting this error
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast

my Code

_data() async {
    setState(() {
      data();
    });
  }

Future<bool> data() async {
final prefs = await SharedPreferences.getInstance();
final key = 'token';
final value = prefs.get(key) ?? 0;
http.Response response = await http.post(
  "https://www.exmaple.com/api/user",
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Bearer $value'
  },
  body: {'key1': value1, 'key2': value2},
);
if (response.statusCode == 200) {
  return true;
} else {
  return false;
 }
}

the above code works when i clicked on a button. See below code

RaisedButton(
              onPressed: () {                    
                _data();
                Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(
                        builder: (BuildContext context) => Home()),
                    (Route<dynamic> route) => false);
              },
              child: Text('Press Me',
                  style: TextStyle(fontSize: 15)),
            ),

See the ScreenShot of error Error ScreenShot

here is error error in line 76

Upvotes: 0

Views: 9450

Answers (3)

John Joe
John Joe

Reputation: 12803

Make sure value1 and value2 is String.

body: {'key1': value1.toString(), 'key2': value2.toString()},

Upvotes: 1

Muhammad Tameem Rafay
Muhammad Tameem Rafay

Reputation: 4585

Please use the Authentication as follows.

'Authorization': 'Bearer '+$value.toString()

Upvotes: 0

Jurkko
Jurkko

Reputation: 78

Don't know which exactly line is throwing you error, but maybe try parsing it ?

var value = int.tryParse(text);

Upvotes: 0

Related Questions