Sam*_*i-L 11 android dart flutter
在针对 Android 模拟器进行调试时,我的应用程序可以成功进行身份验证,但是如果我尝试使用针对物理设备(具有相同操作系统版本)的调试进行身份验证,等待超过一分钟后会出现一条错误消息:
发生异常。
_TypeError(“String”类型不是“index”的“int”类型的子类型)
错误消息指向以下代码:
if (responseJson[AuthUtils.authTokenKey] != null) {
AuthUtils.insertDetails(
userNameController.text, passwordController.text, responseJson);
...
} else {
...
};
Run Code Online (Sandbox Code Playgroud)
在调试控制台中,我得到以下信息:
I/flutter (9531): Auth: SocketException: OS Error: Connection timed out, errno = 110, address = example.com, port = 38975 V/InputMethodManager(9531): 开始输入:tba=android.view.inputmethod.EditorInfo @4aece4e nm:example.com.todoapp ic=null
我在这里做错了什么?
小智 10
对于我的情况 responseJson 是动态数据类型,我所做的是将其转换为字符串。
改变这个
responseJson[AuthUtils.authTokenKey]
对此
String jsonsDataString = responseJson.toString();
final jsonData = jsonDecode(jsonsDataString);
//then you can get your values from the map
if(jsonData[AuthUtils.authTokenKey] != null){
...
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这对我有用。我是在颤抖中这样做的。
http.Response S=await
http.get("Your link here");
String responseBody = S.body;
dynamic jsonObject = jsonDecode(jsonDecode(S.body));
print(jsonObject[0]["product_id"]);
setState(() {
data=jsonObject[0]["product_id"];
});
Run Code Online (Sandbox Code Playgroud)
我相信这一行:
responseJson[AuthUtils.authTokenKey]
Run Code Online (Sandbox Code Playgroud)
您正在通过索引AuthUtils.authTokenKey引用列表responseJson中的项目
由于AuthUtils.authTokenKey是一个字符串,因此您不能将其用作数组的索引。
因此,您需要首先获取AuthUtils.authTokenKey的索引,然后使用它来引用该项目:
responseJson[responseJson.indexOf(AuthUtils.authTokenKey)]
Run Code Online (Sandbox Code Playgroud)
就我而言,我忘记解码 JSON,这就是我收到此类错误的原因。
所以在解析之前不要忘记这样做:
final result = json.decode(response.body);
result[AuthUtils.authTokenKey]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
39510 次 |
最近记录: |