我们在本章回中获取的App自身信息主要指App的包名称,编译版本号等信息。如果是原生开发的话,使用官方提供的接口就可以实现,但是在Flutter开发中没有类似的
接口,怎么办呢?本章回中将给大家介绍如何去获取这些App自身信息。
我们想要获取的App自身信息可以通过package_info_plus这个三方包来实现。该包提供了相关的接口来获取App自身信息。详细的使用方法如下:
App自身的信息以类的成员属性存放在类的对象中,下面是类中的成员属性,请大家参考:
/// The app name. `CFBundleDisplayName` on iOS, `application/label` on Android.
final String appName;
/// The package name. `bundleIdentifier` on iOS, `getPackageName` on Android.
final String packageName;
/// The package version. `CFBundleShortVersionString` on iOS, `versionName` on Android.
final String version;
/// The build number. `CFBundleVersion` on iOS, `versionCode` on Android.
/// Note, on iOS if an app has no buildNumber specified this property will return version
/// Docs about CFBundleVersion: https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleversion
final String buildNumber;
/// The build signature. Empty string on iOS, signing key signature (hex) on Android.
final String buildSignature;
/// The installer store. Indicates through which store this application was installed.
final String? installerStore;
///获取当前app的版本信息
void getPackageInfo() async {
var pkgInfo = await PackageInfo.fromPlatform();
debugPrint("package info: ${pkgInfo.toString()}");
}
上面的示例代码演示了如何获取App自身的信息,编译并且运行上面的程序可以得到下面的运行结果:
///打印出的信息如下:
///appName: fluttercookbook, buildNumber: 1, packageName: com.cookbook.flutter.fluttercookbook, version: 1.0.0
最后,我们对本章回的内容做一个全面的总结: