简体   繁体   中英

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

Make sure value1 and value2 is String .

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

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

var value = int.tryParse(text);

Please use the Authentication as follows.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM