【问题标题】:Flutter app not updating automatically whenever new version is available on google play store每当谷歌 Play 商店有新版本时,Flutter 应用程序不会自动更新
【发布时间】:2021-01-08 10:27:36
【问题描述】:

我希望我的应用程序的用户始终拥有最新版本。如果他们没有最新版本,它应该在应用启动时自动从 Play 商店下载最新版本。我为此使用 in_app_update。我正在表演Performs immediate update 以下是main 之后的启动画面代码。在这里我检查route函数中的更新,如果更新可用则执行更新并导航到homeView,如果不是简单导航到homeView

但每当新版本上传到 Playstore 时,应用程序从未通知新用户更新。他们必须手动去 Playstore 更新应用程序。这是为什么?我在代码中做错了什么还是需要做一些额外的事情?

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(
    .............................
      ),
   );
  }
  }

我不知道为什么类似问题的建议解决方案对我不起作用。 https://stackoverflow.com/a/62129373/7290043

【问题讨论】:

    标签: flutter dart updates google-play-console auto-update


    【解决方案1】:

    在不询问用户的情况下更新应用本身是违反政策的行为,可能会导致您暂停应用。在尝试这样的事情之前阅读此内容:Device and Network Abuse

    只要有新的更新可用,您就可以要求用户更新应用。

    编辑:

    在 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;
      }
    

    手机版: 我用过这个包Package_info

    检查是否有最新版本:

    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);
        }
      }
    

    您可以根据自己的方便设计弹出窗口。

    【讨论】:

    • 是的@Mayur 完全正确。我知道。我正在尝试首先显示提示,如immediate update flow 这个文档截图pub.dev/packages/in_app_update 中提到的那样,但我无法显示该提示
    • @FaizanKamal 我已经用代码更新了我的答案。由于 Playstore 不提供任何 API,因此这是一种解决方法。
    • 感谢您提供如此重要的信息。我现在只有一个困惑。如果(playVersion &gt; CurrentVersion) 为真,那么我将如何在“更新”按钮单击条件内的对话框时更新应用程序。我应该使用 URL 启动器并将它们发送到 Play 商店还是有更好的方法来做到这一点?
    • 推荐使用url启动器,将用户发送到playstore让用户自行更新。
    • 我想知道是否只需在 playVersion &gt; CurrentVersion 的条件下添加此行 InAppUpdate.performImmediateUpdate() 就可以在此处实际工作并向我显示全屏即时更新流程对话框?如这里所示filebin.net/9loybs52eosb03ui/435dV.png?t=9cv2y3tf 上面的行来自这个包pub.dev/packages/in_app_update
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多