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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Yes @Mayur exactly. I know. I;m trying to show the prompt first as mentioned in immediate update flow this docs screenshot pub.dev/packages/in_app_update But I'm unable to show that prompt
@FaizanKamal i have updated my answer with code. as playstore doesn't provide any API here's a workaround.
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?
It's recommended to use url launcher and send user to playstore let user update by their self.
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 filebin.net/9loybs52eosb03ui/435dV.png?t=9cv2y3tf Above line came from this package pub.dev/packages/in_app_update
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.