You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter new_version包抛出RangeError索引越界问题求助

Flutter new_version 包抛出RangeError异常排查

异常信息

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 1
E/flutter (21407): #0 List.[] (dart:core-patch/growable_array.dart:281:36)
E/flutter (21407): #1 NewVersion._getAndroidStoreVersion.

使用new_version包时触发上述未处理异常,我的实现代码如下:

void _checkVersion() async {
  final newVersion = NewVersion(
    androidId: "myId",
  );
  final status = await newVersion.getVersionStatus();
  if (status!.localVersion == status.storeVersion) {
    newVersion.showUpdateDialog(
      context: context,
      versionStatus: status,
      dialogTitle: "UPDATE !!!",
      dismissButtonText: "Skip",
      dialogText: "Please update the app from " +
          "${status.localVersion}" +
          " to " +
          "${status.storeVersion}",
      dismissAction: () {
        Navigator.of(context).pop();
      },
      updateButtonText: "Update Now",
    );
  }

  print("DEVICE : " + status.localVersion);
  print("STORE : " + status.storeVersion);
}

问题分析与修复方案

异常根源

这个错误出现在包内部的_getAndroidStoreVersion方法中,是因为代码尝试访问一个空列表的第1位索引。大概率是以下情况导致:

  • 填的androidId不是Google Play商店中应用的真实包名,导致包请求不到有效版本数据,返回空列表
  • 设备网络无法访问Google Play,请求失败返回空内容
  • 包版本存在bug,没处理空数据的边界情况

修复步骤

  1. 修正androidId
    把androidId改成你应用在Google Play上的真实包名,比如com.yourcompany.yourapp,不能填"myId"这种无效值。

  2. 添加空值安全判断
    原代码直接用status!强制解包,且逻辑写反了(应该是本地版本≠商店版本时才提示更新),修改后代码:

    void _checkVersion() async {
      final newVersion = NewVersion(
        androidId: "你的真实应用包名",
      );
      final status = await newVersion.getVersionStatus();
      // 先判断所有关键数据是否有效
      if (status != null && status.storeVersion != null && status.localVersion != null) {
        // 本地版本和商店版本不一致时才提示更新
        if (status.localVersion != status.storeVersion) {
          newVersion.showUpdateDialog(
            context: context,
            versionStatus: status,
            dialogTitle: "新版本可用!",
            dismissButtonText: "暂不更新",
            dialogText: "请将应用从 ${status.localVersion} 更新到 ${status.storeVersion}",
            dismissAction: () {
              Navigator.of(context).pop();
            },
            updateButtonText: "立即更新",
          );
        }
        print("本地版本 : " + status.localVersion);
        print("商店版本 : " + status.storeVersion);
      } else {
        print("无法获取商店版本信息,请检查网络或包名");
      }
    }
    
  3. 检查网络环境
    确保测试设备能正常访问Google Play商店,国内设备需要特殊网络环境才能请求到数据。

  4. 升级包版本
    去pub.dev查看new_version的最新版本,升级到最新版,可能官方已经修复了空数据解析的bug。

内容的提问来源于stack exchange,提问作者BiNeesH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 16:06:25