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

Flutter中Android与iOS版本更新弹窗实现求助

Flutter 跨平台版本更新弹窗实现求助

我需要在Flutter应用中实现Android和iOS端的版本更新弹窗功能,当有新版本可用时自动提示。尝试多款插件均遇到问题,具体如下:

1. package_info_plus

爬取Google Play页面获取版本号时无法找到指定元素,报错Bad state: No element

代码及报错信息:

try {
  isUpdateAvailable = false;
  final uri = Uri.https("play.google.com", "/store/apps/details", {"id": "${_packageInfo.packageName}"});
  final response = await http.get(uri);
  // print("response is ${response.body}");
  final document = parse(response.body);
  print(1);
  final additionalInfoElements = document.getElementsByClassName('hAyfc');
  print("additionalInfoElements $additionalInfoElements");
  print(2);
  final versionElement = additionalInfoElements.firstWhere((elm) {
    print(elm.querySelector('.BgcNfc')?.text);
    return elm.querySelector('.BgcNfc')?.text == 'Current Version';
  });
  print(3);

  final storeVersion = versionElement.querySelector('.htlgb')?.text ?? "";

  print("storeVersion is $storeVersion");
  print("_packageInfo.version is ${_packageInfo.version}");

  if (_packageInfo.version != storeVersion) {
    isUpdateAvailable = true;
    if (!mounted) return;
    setState(() {});
  }
} on Exception catch (exception) {
  print("exception is $exception");
} catch (err) {
  print("err is ${err}");
}
I/flutter (13572): 1
I/flutter (13572): additionalInfoElements []
I/flutter (13572): 2
I/flutter (13572): err is Bad state: No element

2. in_app_update

调用版本检测时出现绑定服务失败的PlatformException

代码及报错信息:

InAppUpdate.checkForUpdate().then((info) {
  setState(() {
    _updateInfo = info;
  });
}).catchError((e) {
  print("E is $e");
  MPSnackBar.failMsg(context, e.toString());
});
I/PlayCore(13854): UID: [10211]  PID: [13854] AppUpdateService : requestUpdateInfo(com.metalpower.metaCloud)
I/PlayCore(13854): UID: [10211]  PID: [13854] AppUpdateService : Initiate binding to the service.
I/PlayCore(13854): UID: [10211]  PID: [13854] AppUpdateService : Failed to bind to the service.
I/flutter (13854): E is PlatformException(TASK_FAILURE, Failed to bind to the service., null, null)

3. new_version

获取版本状态时出现RangeError索引越界问题

代码及报错信息:

final newVersion = NewVersion(
  iOSId: 'IOS_ID',
  androidId: 'android_id',
);

final status = await newVersion.getVersionStatus();
if (status != null) {
  debugPrint(status.releaseNotes);
  debugPrint(status.appStoreLink);
  debugPrint(status.localVersion);
  debugPrint(status.storeVersion);
  debugPrint(status.canUpdate.toString());
  newVersion.showUpdateDialog(
    context: context,
    versionStatus: status,
    dialogTitle: 'Custom Title',
    dialogText: 'Custom Text',
  );
}
E/flutter (13854): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 1
E/flutter (13854): #0      List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter (13854): #1      NewVersion._getAndroidStoreVersion
package:new_version/new_version.dart:188
E/flutter (13854): <asynchronous suspension>
E/flutter (13854): #2      _SplashScreenUIState.checkStatus
package:metal_power_mobile/…/splash_screen/splash_screen_ui.dart:259
E/flutter (13854): <asynchronous suspension>

4. native_updater

未成功实现功能

5. upgrader

配置GitHub上的appCast.xml后仍无法正常工作

appCast.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
    <channel>
        <title>Help Code TJCODE- Appcast</title>
        <item>
            <title>Version 3.15.0</title>
            <description>desc</description>
            <pubDate>Tue, 08 Oct 2022 12:00:00 +0000</pubDate>
            <enclosure url="https://play.google.com/store/apps/details?id=appId" sparkle:version="1.0.0" sparkle:os="android" />
        </item>
    </channel>
</rss>

已被困4天,恳请提供可行的Flutter版本更新弹窗实现方案,谢谢。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 20:25:27