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

Flutter应用如何检测Build Number并触发更新提醒?

Flutter应用检测Build Number实现更新提醒方案

默认的upgrader、new_version等包仅校验版本名(如2.0.0),不会解析版本号后缀的Build Number(如+99/+100)。要实现Build Number维度的更新检测,需要自定义版本校验逻辑,核心步骤如下:

1. 获取本地应用的Build Number

使用package_info_plus包获取本地应用的Build Number(对应Android的versionCode、iOS的Build号):

import 'package:package_info_plus/package_info_plus.dart';

Future<int> getLocalBuildNumber() async {
  final packageInfo = await PackageInfo.fromPackage();
  // 将Build Number转为整数(通常为数字格式)
  return int.parse(packageInfo.buildNumber);
}

2. 获取应用商店的最新Build Number

由于Google Play和App Store的公开接口不直接返回Build Number,推荐通过自定义后端接口维护最新版本信息(每次发版后手动更新接口数据),避免爬取商店页面被限制:

import 'package:http/http.dart' as http;
import 'dart:convert';

Future<int> getStoreBuildNumber() async {
  // 替换为你的后端接口地址,接口返回格式示例:{"buildNumber": 100}
  final response = await http.get(Uri.parse('https://your-api-domain.com/app/latest-build'));
  
  if (response.statusCode == 200) {
    final data = json.decode(response.body);
    return data['buildNumber'] as int;
  } else {
    // 网络异常时默认不触发更新
    return -1;
  }
}

如果必须对接官方API:

  • Android:使用Google Play Developer API的edits.tracks.get接口,需配置OAuth2认证,适合企业级场景;
  • iOS:使用App Store Connect API的builds端点,需生成API密钥并完成服务器端认证。

3. 自定义版本对比与更新提醒

对比本地和商店的Build Number,若商店版本更高则触发弹窗提醒,并引导用户跳转至应用商店:

import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

void checkForBuildUpdate(BuildContext context) async {
  final localBuild = await getLocalBuildNumber();
  final storeBuild = await getStoreBuildNumber();
  
  if (storeBuild > localBuild && storeBuild != -1) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (ctx) => AlertDialog(
        title: const Text('应用更新提醒'),
        content: const Text('检测到应用新版本,请前往应用商店更新'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: const Text('稍后更新'),
          ),
          TextButton(
            onPressed: () async {
              await _openAppStore(context);
              Navigator.pop(ctx);
            },
            child: const Text('立即更新'),
          ),
        ],
      ),
    );
  }
}

Future<void> _openAppStore(BuildContext context) async {
  final packageInfo = await PackageInfo.fromPackage();
  final Uri url;
  
  if (Theme.of(context).platform == TargetPlatform.android) {
    // 跳转Google Play
    url = Uri.parse('market://details?id=${packageInfo.packageName}');
  } else {
    // 跳转App Store,替换为你的应用ID
    url = Uri.parse('https://apps.apple.com/cn/app/your-app-name/id123456789');
  }
  
  if (await canLaunchUrl(url)) {
    await launchUrl(url);
  }
}

4. 集成到应用生命周期

在应用启动时触发检测,比如在initState或main函数中调用:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // 延迟1秒检测,避免启动卡顿
    Future.delayed(const Duration(seconds: 1), () {
      checkForBuildUpdate(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const HomePage(),
    );
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 00:11:04