How to force users to update app from play store in Flutter using in_app_update

Viewed 18911

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 first as shown in the image.

Forced Update

I found a package called Upgrader but this just gives a prompt. It is not linked to play store as shown in image.

Edit

As suggested by Maadhav Sharma, now I am using the in_app_update package but it says no update available.

This is my code:

....

class _TnPState extends State<TnP> {
  ThemeBloc _themeBloc;
  FirebaseMessaging _firebaseMessaging;
  NotificationBloc _notificationBloc;
  String _test;

  @override
  void initState() {
    _themeBloc = ThemeBloc();
    _firebaseMessaging = FirebaseMessaging();
    _notificationBloc = NotificationBloc();
    _firebaseMessaging.configure(
      onMessage: (notification) async => _notificationBloc.yes(),
      onResume: (notification) async => _notificationBloc.yes(),
      onLaunch: (notification) async => _notificationBloc.yes(),
    );
    checkForUpdate();
    super.initState();
  }

  Future<void> checkForUpdate() async {
    InAppUpdate.checkForUpdate().then((info) {
      String display = info.toString();
      setState((){
        _test = display;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<AppTheme>(
      stream: _themeBloc.themeStream,
      initialData: AppTheme.Light,
      builder: (context, AsyncSnapshot<AppTheme> snapshot) {
        return MaterialApp(
          title: 'TnP',
          debugShowCheckedModeBanner: false,
          theme: appThemeData[snapshot.data],
          home: _test==null ?
                Container() : 
                InAppUpdateScreen(display: _test),//SplashScreen(),
        );
      },
    );
  }
}

InAppUpdateScreen just displays the text.
App in play store shows update is available:
App in play store


But the app which is an older version installed via playstore shows no update:
No update in app


Any idea how to solve this?

3 Answers

Might not be the actual answer for the question but if someone is looking for an easy to integrate built in solution, which works for both Android, iOS and is language, framework agnostic. You can use https://appupgrade.dev/ service to force update you mobile apps. You need to create new version for your app versions you want to update in the app upgrade service and select whether you want to force it or just want to let users know that new version is available.

enter image description here

after this you will need to call the appupgrade api from your app with the required details such as your app version, platform, environment and app name. The API will return you the details.. that this app needs to be updated or not. Based on the response you can show popup in your app.You can call this API when app starts or periodically to check for the update. You can even provide a custom message. API response:

enter image description here

See the response has force update true. So handle in the app by showing popup. enter image description here

You can find the complete user documentation here. https://appupgrade.dev/docs

Thanks.

You can use this package
https://pub.dev/packages/upgrader

Wrap your whole widget in UpgradeAlert widget like this ->

Scaffold(
          appBar: AppBar(title: Text('Upgrader Example')),
          body: UpgradeAlert(
            child: Center(child: Text('Your widget is here')),
          )),

if an update available

On Ios style On Android Style

Related