简体   繁体   English

Flutter [错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:类型“int”不是类型转换中类型“String”的子类型

[英]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我正在从我的 flutter 应用程序发送 api 请求以更新用户数据并收到此错误
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast [错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:类型'int'不是类型转换中类型'String'的子类型

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查看错误的截图Error ScreenShot

here is error error in line 76这是第 76 行的错误错误

Make sure value1 and value2 is String .确保value1value2String

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:flutter/lib/ui/ui_dart_state.cc(148) 未处理的异常 - ERROR:flutter/lib/ui/ui_dart_state.cc(148) Unhandled Exception E/flutter (8084): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:SocketException:操作系统错误:连接被拒绝 - E/flutter ( 8084): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused 发布序列化 Json 正文 - [VERBOSE-2:ui_dart_state.cc(177)] 未处理的异常:错误 - - Post serialised Json body - [VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Error- “[错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:类型&#39;未来<File> &#39; 不是类型“文件?”的子类型在类型转换中“错误 - "[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<File>' is not a subtype of type 'File?' in type cast" Error webView 在颤振中不起作用 [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] - webView don't work in flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:类型“String”不是类型“Map”的子类型<string, dynamic></string,> - ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic> 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型转换 int flutter 中类型 'OriginDestination' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter 未处理的异常:“String”类型不是 dart 中“index”错误的“int”类型的子类型 - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' error in dart 未处理的异常:类型&#39;String&#39;不是颤振中&#39;index&#39;的&#39;int&#39;类型的子类型 - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' in flutter Flutter 错误:“int”类型不是类型转换中“String”类型的子类型 - Flutter error: type 'int' is not a subtype of type 'String' in type cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM