首页 > 解决方案 > 如何使用 in_app_update 强制用户从 Flutter 中的 Play 商店更新应用

问题描述

我希望我的应用程序的用户始终拥有最新版本。如果他们没有最新版本,它应该首先从 Play 商店下载最新版本,如图所示。

强制更新

我找到了一个名为Upgrader的软件包,但这只是提示。如图所示,它没有链接到 Play 商店。

编辑

正如Maadhav Sharma所建议的,现在我正在使用in_app_update包,但它说没有可用的更新

这是我的代码:

....

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只显示文本。
Play商店中的应用程序显示更新可用:
Play商店中的应用程序


但是通过 playstore 安装的旧版本的应用程序没有显示更新:
应用中没有更新


知道如何解决这个问题吗?

标签: androidfluttergoogle-playin-app-update

解决方案


有一个你想要的包:

https ://pub.dev/packages/in_app_update
在此处输入图像描述

Android
此插件集成了官方 Android API 以在 2019 年发布的应用更新中执行:https ://developer.android.com/guide/app-bundle/in-app-updates

iOS
iOS 不提供此类功能。您可能想查看例如https://pub.dev/packages/upgrader。如果您在 iOS 设备上调用上述方法,您将遇到未实现的异常。

谢谢...


推荐阅读