我希望我的应用程序的用户始终拥有最新版本。如果他们没有最新版本,它应该在应用程序启动时自动从 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
在不询问用户的情况下更新应用程序本身是违反政策的,可能会导致你暂停应用程序。在尝试这样的事情之前先阅读这个:设备和网络滥用
你可以要求用户在有新更新可用时更新应用程序。
编辑:
在 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 > CurrentVersion)
为真,那么我将如何在 if 条件内单击对话框的“更新”按钮上更新应用程序。我应该使用 URL 启动器并将它们发送到 Play 商店还是有更好的方法来做到这一点?建议使用 url 启动器并将用户发送到 Playstore 让用户自行更新。