Warm tip: This article is reproduced from serverfault.com, please click

dart-每当 Google Play 商店有新版本可用时,Flutter 应用程序不会自动更新

(dart - Flutter app not updating automatically whenever new version is available on google play store)

发布于 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

Questioner
Faizan Kamal
Viewed
0
Mayur Chaudhary 2021-01-08 20:55:47

在不询问用户的情况下更新应用程序本身是违反政策的,可能会导致你暂停应用程序。在尝试这样的事情之前先阅读这个:设备和网络滥用

你可以要求用户在有新更新可用时更新应用程序。

编辑:

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

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