Flutter -如何检查应用程序是否有新版本?

rryofs0p  于 2022-12-24  发布在  Flutter
关注(0)|答案(6)|浏览(172)

我用**Flutter**创建了一个应用。我需要强制用户在我上传新版本时更新应用。我尝试了this package,但我认为它不起作用。在文档中,我们可以看到我们可以使用此代码验证版本。

Container(
     margin: EdgeInsets.fromLTRB(12.0, 0.0, 12.0, 0.0),
     child: UpgradeCard();
)

问题是:我需要在 *"initState"中检查版本,而不是在Scaffold中。那么在应用启动时检查版本的正确方法是什么?

xn1cxnb4

xn1cxnb41#

我建议试用Firebase Remote Config并在那里更新最低要求的版本。在商店中的每次更新时强制更新应用程序不是一个好做法。此外,一些用户在给定的商店中看到更新版本的时间可能比其他用户晚。这正是Google Play和AppStore的工作原理。
因此,当您确实需要强制更新应用程序时,您可以在Firebase上增加该参数,例如在商店中更新后一天。
触发对话框的简单代码如下所示:

Future<bool> checkUpdates() async {
    await remoteConfig.fetch();
    await remoteConfig.activateFetched();

    final requiredBuildNumber = remoteConfig.getInt(Platform.isAndroid
        ? 'requiredBuildNumberAndroid'
        : 'requiredBuildNumberIOS');

    final currentBuildNumber = int.parse(packageInfo.buildNumber);

    return currentBuildNumber < requiredBuildNumber;
  }

这需要添加package_info软件包以及firebase_remote_config
要在商店中打开应用程序,您需要使用url_launcher包,只需将URL传递到您的应用程序。

1l5u6lss

1l5u6lss2#

你可以打电话给https://itunes.apple.com/lookup?bundleId=$idhttps://play.google.com/store/apps/details?id=$id来接收商店上可用的最新应用程序版本。我可以推荐这个软件包来做到这一点:https://pub.dev/packages/new_version

z31licg0

z31licg03#

您可以按以下方式检查应用版本

@override
void initState() {
super.initState();
Timer( 
    Duration(seconds: 3), // put your stuff here
        () => Navigator.of(context).pushReplacement(MaterialPageRoute(
        builder: (BuildContext context) => LoginScreen())));}

假设一件事,这个代码必须在spalsh屏幕或主屏幕,在那里你可以检查应用程序版本强制停止任何应用程序属于旧版本

myzjeezk

myzjeezk4#

试用in应用程序更新包-https://pub.dev/packages/in_app_update,它适用于Android,对于iOS,请试用-https://pub.dev/packages/upgrader

o0lyfsai

o0lyfsai5#

使用这个软件包-〉new_version它是可怕的和容易:)

///initialize your variable
    final newVersion = NewVersion(
      iOSId: 'com.google.Vespa',
      androidId: 'com.google.android.apps.cloudconsole',
    );

//并在小部件或控制器内部使用它

@override
    Widget build(BuildContext context) {
        newVersion.showAlertIfNecessary(context: context);
        return Container();
    }

8nuwlpux

8nuwlpux6#

如果你只想从App Store和Play Store获取当前版本号,你可以使用upgrader软件包API,如下所示:

import 'dart:developer';
import 'dart:io';
import 'package:html/dom.dart';
import 'package:upgrader/upgrader.dart';

Future<String?> getStoreVersion(String myAppBundleId) async {
  String? storeVersion;
  if (Platform.isAndroid) {
    PlayStoreSearchAPI playStoreSearchAPI = PlayStoreSearchAPI();
    Document? result = await playStoreSearchAPI.lookupById(myAppBundleId, country: 'US');
    if (result != null) storeVersion = PlayStoreResults.version(result);
    log('PlayStore version: $storeVersion}');
  } else if (Platform.isIOS) {
    ITunesSearchAPI iTunesSearchAPI = ITunesSearchAPI();
    Map<dynamic, dynamic>? result = 
                    await iTunesSearchAPI.lookupByBundleId(myAppBundleId, country: 'US');
    if (result != null) storeVersion = ITunesResults.version(result);
    log('AppStore version: $storeVersion}');
  } else {
    storeVersion = null;
  }
  return storeVersion;
}

编辑:如果您的应用程序在美国应用商店或Play商店不可用,您需要更改国家代码。

相关问题