类型'String'不是'index'类型'int'的子类型

问题描述 投票:2回答:2

我的应用程序可以在针对Android模拟器进行调试时成功进行身份验证,但如果我尝试使用针对物理设备的调试进行身份验证(具有相同的操作系统版本),则会在超过一分钟的等待后显示错误消息:

发生了例外情况。

_TypeError(类型'String'不是'index'的'int'类型的子类型)

错误消息指向以下代码:

        if (responseJson[AuthUtils.authTokenKey] != null) {
          AuthUtils.insertDetails(
              userNameController.text, passwordController.text, responseJson);
...
        } else {
...
        };

在DEBUG CONSOLE我得到以下内容:

I / flutter(9531):Auth:So​​cketException:OS Error:连接超时,errno = 110,address = example.com,port = 38975 V / InputMethodManager(9531):开始输入:tba = android.view.inputmethod.EditorInfo @ 4aece4e nm:example.com.todoapp ic = null

这是截图:enter image description here

我在这做错了什么?

android dart flutter
2个回答
3
投票

我相信在线:

responseJson[AuthUtils.authTokenKey]

您通过索引AuthUtils.authTokenKey引用列表responseJson中的项

由于AuthUtils.authTokenKey是一个String,因此不能将其用作数组的索引。

因此,首先需要获取AuthUtils.authTokenKey的索引,然后使用它来引用该项:

responseJson[responseJson.indexOf(AuthUtils.authTokenKey)]

0
投票

我有一个与此非常相似的问题。它是由对条件的条件赋值引起的,该条件导致类型在运行时更改。看一下NetworkUtil.authencateUser返回的内容。我敢打赌,这是未来尝试将其改为未来>。

你的模拟器有不同的SHA1密钥吗?这可能是两种环境之间差异的原因。


0
投票

对于我的案例,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){
...
}
© www.soinside.com 2019 - 2024. All rights reserved.