0

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

Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56

1 Answers1

2

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.

Mayur Chaudhary
  • 217
  • 1
  • 14
  • Yes @Mayur exactly. I know. I;m trying to show the prompt first as mentioned in `immediate update flow` this docs screenshot https://pub.dev/packages/in_app_update But I'm unable to show that prompt – Faizan Kamal Jan 08 '21 at 12:32
  • 1
    @FaizanKamal i have updated my answer with code. as playstore doesn't provide any API here's a workaround. – Mayur Chaudhary Jan 08 '21 at 12:56
  • Thanks for such a vital info. I only have one confusion now. If `(playVersion > CurrentVersion)` is true then how I'm gonna update app on "update" button click of dialog inside if condition. Should I use URL launcher and send them to play store or is there a better way to do this? – Faizan Kamal Jan 08 '21 at 17:22
  • It's recommended to use url launcher and send user to playstore let user update by their self. – Mayur Chaudhary Jan 08 '21 at 17:36
  • I'm wondering if simply adding this line `InAppUpdate.performImmediateUpdate()` inside if condition of `playVersion > CurrentVersion` will actually work here and show me the full screen immediate update flow dialog or not? As shown here https://filebin.net/9loybs52eosb03ui/435dV.png?t=9cv2y3tf Above line came from this package https://pub.dev/packages/in_app_update – Faizan Kamal Jan 08 '21 at 17:42
  • And btw `newVersion` is used as the parameter of `getUpdateInfo` in above. Was it a mistake? If not, from where that `newVersion` came from? – Faizan Kamal Jan 08 '21 at 17:47
  • And also in this line `final document = parse(response.body);`, I'm getting error `The method 'parse' isn't defined` – Faizan Kamal Jan 08 '21 at 17:55
  • replacing `parse` with `json.jsonDecode` giving `FormatException: Unexpected character` – Faizan Kamal Jan 08 '21 at 18:09
  • I figured it out. Thanks for the help – Faizan Kamal Jan 16 '21 at 18:59