10

I create an app with Flutter. I need to force users to update the app when I upload a new version. I try this package, but I think that it doesn´t work. In the documentation, we can read that we can verify the version with this code.

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

The problem is: I need to check the version in *'initState', not in Scaffold. So what is the right way to check the version on the app start?

8 Answers 8

17

I recommend trying out the Firebase Remote Config and update the minimum required version there. It's not a good practice to force update app on every update in the store. Also, some users can see the updated version in given store later than others. This is just how Google Play and AppStore work.

Thus, when you really need to force update the application you can increment the parameter on Firebase e.g. a day after update in the store.

Simple code to trigger the dialog can look as following:

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;
  }

This requires package_info package to be added as well as firebase_remote_config.

To open the app in the store you need to use url_launcher package and just pass URL to your app.

2
  • will this work for android though, where build numbers are 1.0.1 etc?
    – cfl
    Commented May 1, 2020 at 21:27
  • 1
    You refer to build version. Build numbers are just integers. Commented May 1, 2020 at 21:37
6

you can call https://itunes.apple.com/lookup?bundleId=$id or https://play.google.com/store/apps/details?id=$id to receive the newest app version available on the stores. i can recommend this package to do this: https://pub.dev/packages/new_version

1
  • 1
    how do we get latest version from these links?
    – Janaka
    Commented Jun 14, 2021 at 19:18
3

You can check your app version like

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

and presume one thing this code must be in spalsh screen or home screen and there you can check app version forcefully stop any app which is belonged old version

3
  • And how can i add UpgradeCard() inside this timer? Commented Sep 23, 2019 at 7:55
  • I try Timer( Duration(seconds: 10), UpgradeAlert()); and return The argument type 'UpgradeAlert' can't be assigned to the parameter type 'void Function()'. Commented Sep 23, 2019 at 8:07
  • 1
    you have to match app version and current version of the app . Commented Sep 23, 2019 at 8:47
2

Try out the in app update package- https://pub.dev/packages/in_app_update which works on Android and for iOS, try- https://pub.dev/packages/upgrader.

2
2

If you just want the current version number from the App Store and Play Store. You can use the upgrader package API like this:

EDIT: If your app is NOT available on the US App Store or Play Store, you need to change the country code.

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 = playStoreSearchAPI.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 = iTunesSearchAPI.version(result);
    log('AppStore version: $storeVersion}');
  } else {
    storeVersion = null;
  }
  return storeVersion;
}
1

Use this package -> new_version It's awesome & easy :)

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

//and use this inside of your widget or controller

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

enter image description here

0

Try out flutter_upgrade_version 1.1.3 which support get Package Information, Information of Version on AppStore, Support In App Update - Android.

Details have been described in package

Android: Using the in-app updates feature is supported.

InAppUpdateManager manager = InAppUpdateManager();
AppUpdateInfo? appUpdateInfo = await manager.checkForUpdate();

/// Using `appUpdateInfo.updateAvailability` to check for update availability

iOS: Using iTunes Search API to get the newest app version available on the stores.


PackageInfo _packageInfo = await PackageManager.getPackageInfo();

if (Platform.isIOS) {
  VersionInfo? _versionInfo2 = await UpgradeVersion.getiOSStoreVersion(
    packageInfo: _packageInfo, 
    regionCode: 'VN',  //RegionCode where your app will be available to purchase or download. 
  );
  ///Example: VN - Viet Nam
}
0

Its recommended to use https://firebase.google.com/docs/remote-config to configure the minimum supported version.

Once done, you need a code like below to check if forceUpdate is required.

Future<bool> _forceUpdateRequired() async {
    final isIOS = defaultTargetPlatform == TargetPlatform.iOS;
    final isAndroid = defaultTargetPlatform == TargetPlatform.android;
    final iOSMinSupportedVersion = // if isIOS then fetch ios min supported version from firebase remote config
    final androidMinSupportedVersion = //if isAndroid then fetch android min supported version from firebase remote config
    // use PackageInfo to get current app version
    final packageInfo = await PackageInfo.fromPlatform();
    // and now use Version api to compare the version against min supported version
    return version.Version.parse(packageInfo.version) < version.Version.parse(isIOS ? iOSMinSupportedVersion : androidMinSupportedVersion);
}

Refer https://pub.dev/documentation/package_info/latest/

Refer https://pub.dev/packages/version

Not the answer you're looking for? Browse other questions tagged or ask your own question.