【问题标题】:Flutter app error Exception has occurred. _TypeError (type 'String' is not a subtype of type 'int' of 'index')Flutter 应用错误 发生异常。 _TypeError(类型'String'不是'index'类型'int'的子类型)
【发布时间】:2020-08-09 12:47:37
【问题描述】:

尝试在颤振中运行时出现此异常错误(发生了异常。_TypeError(类型'String'不是'index'类型'int'的子类型)错误是行 serverToken = jsonDecode(data)[ “钥匙”];

 void getFCMServerKey() async {
     final RemoteConfig remoteConfig = await RemoteConfig.instance;
    await remoteConfig.fetch(expiration: const Duration(hours: 5));
    await remoteConfig.activateFetched();
     var data = remoteConfig.getString('FcmServerKey');
     if (data != null) {
       serverToken = jsonDecode(data)["key"];
     }   }

【问题讨论】:

  • 错误是哪一行?
  • 抱歉有效点!错误在线 serverToken = jsonDecode(data)["key"];

标签: flutter dart


【解决方案1】:

我不知道你为什么要写这个["key"]

改为这样做:

serverToken = jsonDecode(data);

【讨论】:

    【解决方案2】:

    这是错误的,因为当你将 json 数据转换为 Dart 类 jsonDecode(data) 时,你得到了一个 List。可能你假设得到一个Map 并尝试使用key 参数访问项目并得到一个错误,因为你不能使用你应该使用int 索引号的字符串键访问List 的项目。在尝试访问项目之前,请确保您的数据采用您想要的格式。

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2021-03-29
      • 2020-04-23
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2023-02-10
      • 2021-01-25
      • 2020-04-19
      相关资源
      最近更新 更多