【问题标题】:Type 'String' is not a subtype of type 'int' of 'index'“String”类型不是“index”的“int”类型的子类型
【发布时间】:2019-04-24 06:46:40
【问题描述】:

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

发生异常。

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

错误信息指向如下代码:

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

在调试控制台中,我得到以下信息:

I/flutter (9531): Auth: SocketException: OS Error: Connection timed 出,errno = 110,地址 = example.com,端口 = 38975 V/InputMethodManager(9531):开始输入: tba=android.view.inputmethod.EditorInfo@4aece4e nm : example.com.todoapp ic=null

截图如下:

我在这里做错了什么?

【问题讨论】:

  • responseJson 似乎被推断为List 类型,并且在索引访问器responseJson[intIndexhere] 中需要一个整数,但AuthUtils.authTokenKeyString,此处不支持。仅当 responseJsonMap 时才支持此方法

标签: android dart flutter


【解决方案1】:

对于我来说 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){
...
}

【讨论】:

    【解决方案2】:

    这对我有用。我在颤抖中这样做。

      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"];
      });
    

    【讨论】:

    • 您必须为该响应字符串创建模型并使用该值..
    • if(response.statusCode == 200){ var responseString = response.body; print("responseString 是 $responseString"); var valueMap = json.decode(responseString);打印(值映射); return WithdrawResponseModel.fromJson(json.decode(valueMap)); }else{ 返回空值; }
    【解决方案3】:

    我相信在这条线上:

    responseJson[AuthUtils.authTokenKey]
    

    您通过索引 AuthUtils.authTokenKey

    引用列表 responseJson 中的项目

    并且由于 AuthUtils.authTokenKey 是一个字符串,因此您不能将其用作数组的索引。

    所以你需要先获取AuthUtils.authTokenKey的索引,然后用它来引用item:

    responseJson[responseJson.indexOf(AuthUtils.authTokenKey)]
    

    【讨论】:

    • 尝试了您的解决方案,但在屏幕截图中显示错误消息,我得到以下信息:无法加载源'dart:core/runtime/libstring_patch.dart':。
    【解决方案4】:

    'String' 不是 'index' 的 'int' 类型的子类型 直接表示您在这里使用 String 代替 Int:responseJson[AuthUtils.authTokenKey] 将 int 作为索引在哪里 -> AuthUtils.authTokenKey 是一个 String 。 *

    在控制台中打印您的 response.body 并检查您的 authTokenKey 已存储。

    * 您可以在响应中使用 indexOf 直接获取其索引,并在响应中用作获取 authTokenKey 的索引

    【讨论】:

      【解决方案5】:

      在 Firebase Firestore 中测试数据库时出现此错误。

      以下断言被抛出构建 StreamBuilder(脏,状态: _StreamBuilderBaseState>#52d4b):类型“字符串”不是子类型 'int'类型的

      问题是我的文档字段有一个字符串类型,但它需要一个数字。所以,从字符串更改为数字,它就起作用了。

      【讨论】:

        【解决方案6】:

        在我的情况下,我忘记解码 JSON,这就是我收到此类错误的原因。

        所以在解析之前不要忘记这样做:

        final result = json.decode(response.body);
        
        result[AuthUtils.authTokenKey]
        

        【讨论】:

        • 你确定吗?
        【解决方案7】:

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

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

        【讨论】:

          【解决方案8】:

          检查响应是否返回 Array 数据。

          {[{authTokenKey: 'asbc'}]}
          

          在这种情况下,你需要取 [0] 元素。然后你会得到一个 Map ,你可以在其中访问带有字符串值的索引。

          var authTokenElm = responseJson[0];
          var authToken = authTokenElm[AuthUtils.authTokenKey];
          

          【讨论】:

            【解决方案9】:
            $json_encode = json_encode($query_array);
            $json_encode = str_replace("[", "", $json_encode);
            $json_encode = str_replace("]", "", $json_encode);
            echo $json_encode;
            

            【讨论】:

              【解决方案10】:

              对于那些提出 API 发布/放置请求的人,请发送地图,

              final payload = {data: [Array should be sent in this format]}
              

              当您想要将 Array of Map 发送到 API 主体时。

              【讨论】:

                猜你喜欢
                • 2021-03-29
                • 1970-01-01
                • 1970-01-01
                • 2020-04-22
                • 2021-08-10
                • 2021-09-08
                • 2020-10-02
                • 1970-01-01
                • 2022-09-27
                相关资源
                最近更新 更多