flutterin-app-updateapp-update

Perform in app force update using current and required build numbers


I want to force update my app.

Here's what I have done so far.

  1. Obtained the current build version of my app using package_info_plus
  2. Obtained the enforced build version which I have stored in the firebase remote config. So I used this package: firebase_remote_config

I then compared the two build numbers to see if the update is needed. What should I do after that?

Here's my code:

 void initState(){ 
     super.initState();
     checkForUpdate();
    _initPackageInfo();
    _enforcedVersion();

if(int.parse(_packageInfo.buildNumber) > int.parse(enforcedBuildNumber))
{ 
    //How to force update? 


}
}

Future<void> _initPackageInfo() async {
    final info = await PackageInfo.fromPlatform();
    setState(() {
      _packageInfo = info;
    });
  }
Future<void> _enforcedVersion() async {
  final RemoteConfig remoteConfig =  RemoteConfig.instance;
  await remoteConfig.setConfigSettings(RemoteConfigSettings(
              fetchTimeout: const Duration(seconds: 10),
              minimumFetchInterval: Duration.zero,
            ));
  await remoteConfig.fetchAndActivate();
   setState(() {
     enforcedBuildNumber = remoteConfig.getString('enforced_build_number');
    });
}

Solution

  • You could display a non dismissable dialog which would ask the user to update the application with a redirection button to the device appstore.

    By using a package such as url_launcher you can easily do that:

    Code Sample

    import 'dart:io' show Platform;
    
    import 'package:url_launcher/url_launcher.dart';
    
    // You can show a dialog like this
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (_) => AlertDialog(
        title: Text('Please update your app'),
        actions: [
          TextButton(
            onPressed: launchAppStore,
            child: Text('Open App Store'),
          ),
        ],
      ),
    );
    
    // Method to open the appstore
    void launchAppStore() {
      /// Depending on where you are putting this method you might need
      /// to pass a reference from your _packageInfo.
      final appPackageName = _packageInfo.packageName;
    
      if (Platform.isAndroid) {
        launch("https://play.google.com/store/apps/details?id=$appPackageName");
      } else if (Platform.isIOS) {
        launch("market://details?id=$appPackageName");
      }
    }