问题描述
此处的关键字是网络.我可以使用 package_info 插件来获取Flutter应用程序的内部版本号和版本号.在网络上无法运行.如何获取Flutter Web应用程序的软件包信息?
The keyword here is Web. I can get the build and version number of a Flutter app by using the package_info plugin, but if it is running on the web I can't. How do I get the package info for a Flutter Web app?
我将在下面提供一个答案作为临时解决方案,但我正在寻找一个从pubspec.yaml获取版本信息的解决方案.
I'll include an answer below as a temporary fix, but I am looking a solution that gets the version info from pubspec.yaml.
推荐答案
作为临时的解决方法,您可以创建一个单独的文件,其中包含版本信息:
As a temporary workaround you can create a separate file with the version info in it:
web_version_info.dart
class WebVersionInfo {
static const String name = '1.0.0';
static const int build = 1;
}
您可以将其用于所有平台,也可以在代码中使用kIsWeb
仅将其用于网络:
You can use that for all platforms or in your code you can use kIsWeb
to just use it for the web:
Future<String> _getAppVersion() async {
if (kIsWeb) {
return WebVersionInfo.name;
} else {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
return packageInfo.version;
}
}
当然,这不是一个很好的解决方案,因为现在您每次更新应用程序时都需要记住要更新版本并在pubspec.yaml
和WebVersionInfo
中构建信息.
Of course, this is not a great solution because now you need to remember to update the version and build information in both pubspec.yaml
and in WebVersionInfo
every time you update the app.
这篇关于如何获取Flutter Web应用程序的内部版本和版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!