Warm tip: This article is reproduced from serverfault.com, please click

Flutter app not updating automatically whenever new version is available on google play store

发布于 2021-01-08 10:27:36

I want the users of my app to always have the latest version. If they don't have the latest version, it should download the latest version from play store automatically on app startup. I'm using in_app_update for that. I'm performing Performs immediate update Below is the code of splash screen which came after main. Here I check for update in route function, if update is available then perform update and navigate to homeView, If not simply navigate to homeView

But app never informed new user about update whenever new version is uploaded on playstore. They have to manually go to playstore to update an app. Why is that? Am I doing something wrong in a code or do I need to do something extra?

import 'package:in_app_update/in_app_update.dart';

class SplashView extends StatefulWidget {
  @override
  _SplashViewState createState() => _SplashViewState();
}

class _SplashViewState extends State<SplashView> {
  AppUpdateInfo _updateInfo;   // --- To check for update
  
@override
  void initState() {
    super.initState();
    startTimer();
  }

  startTimer() async {
    var duration = Duration(milliseconds: 1500);
    return Timer(duration, route);
  }

  route() async {
    await checkForUpdate();
    bool visiting = await ConstantsFtns().getVisitingFlag();
    if (_updateInfo?.updateAvailable == true) {
      InAppUpdate.performImmediateUpdate().catchError((e) => _showError(e));
          Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => HomeView(),
          ),
        );
    } else {
            Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => HomeView(),
          ),
        );
    }
  }

  Future<void> checkForUpdate() async {
    InAppUpdate.checkForUpdate().then((info) {
      setState(() {
        _updateInfo = info;
      });
    }).catchError((e) => _showError(e));
  }

  void _showError(dynamic exception) {
 _scaffoldKey.currentState.showSnackBar(SnackBar(
      content:
          Text("Exception while checking for error: " + exception.toString()),
    ));

 @override
  Widget build(BuildContext context) {
    return Material(
      child: AnimatedSplashScreen(
    .............................
      ),
   );
  }
  }

I don't know why suggested solution of similar question is not working for me. https://stackoverflow.com/a/62129373/7290043

Questioner
Faizan Kamal
Viewed
11
Mayur Chaudhary 2021-01-08 20:55:47

By updating app itself without asking user is policy violation that may lead you to suspension of app. read this before trying anything like this: Device and Network Abuse

You can ask users to update app whenever new update is available.

Edit:

code for Finding latest version on playstore:

getAndroidStoreVersion(String id) async {
    final url = 'https://play.google.com/store/apps/details?id=$id';
    final response = await http.get(url);
    if (response.statusCode != 200) {
      print('Can\'t find an app in the Play Store with the id: $id');
      return null;
    }
    final document = parse(response.body);
    final elements = document.getElementsByClassName('hAyfc');
    final versionElement = elements.firstWhere(
      (elm) => elm.querySelector('.BgcNfc').text == 'Current Version',
    );
    dynamic storeVersion = versionElement.querySelector('.htlgb').text;

    return storeVersion;
  }

For mobile version: i have used this package Package_info

To check if latest version is available or not:

getUpdateInfo(newVersion) async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String playStoreVersion =
        await getAndroidStoreVersion(packageInfo.packageName);
    int playVersion = int.parse(playStoreVersion.trim().replaceAll(".", ""));
    int CurrentVersion=
        int.parse(packageInfo.version.trim().replaceAll(".", ""));
    if (playVersion > CurrentVersion) {
      _showVersionDialog(context, packageInfo.packageName);
    }
  }

You can design your pop up as per your convenience.