The package_info_plus package provides an API for querying information about an application package, such as CFBundleVersion on iOS or versionCode on Android.
import'package:package_info_plus/package_info_plus.dart';...// Be sure to add this line if `PackageInfo.fromPlatform()` is called before runApp()WidgetsFlutterBinding.ensureInitialized();...PackageInfo packageInfo = await PackageInfo.fromPlatform();String appName = packageInfo.appName;String packageName = packageInfo.packageName;String version = packageInfo.version;String buildNumber = packageInfo.buildNumber;Future<PackageInfo> fromPlatform({ String? baseUrl,})fromPlatform method retrieves package information from the platform. The result is cached. Note that the baseUrl parameter is for web use only and the other platforms will ignore it.
Calling to
PackageInfo.fromPlatform()before therunApp()call will cause an exception.
We can use the PackageInfo to query information about the application package.
• appName• packageName• version• buildNumber• buildSignature• installerStore• installTime• updateTime• runtimeType
Installer Store
The installerStore property indicates which app store installed the application. This is useful for directing users to the appropriate store page for ratings or updates.
PackageInfo packageInfo = await PackageInfo.fromPlatform();String? installerStore = packageInfo.installerStore;Note that installerStore returns null on MacOS, Linux, Windows, and Web.
iOS
The installerStore value on iOS is determined by checking the app store receipt path:
installerStore | |
| App Store | com.apple |
| TestFlight | com.apple.testflight |
| Simulator | com.apple.simulator |
If the package returns incorrect app version, Flutter build tools allow only digits and . (dot) symbols to be used in version of pubspec.yaml on iOS/macOS to comply with official version format from Apple.
If we changed version in pubspec.yaml and this package returns wrong info, we should check that package_info_plus on iOS requires the Xcode build folder to be rebuilt after changes to the version string in pubspec.yaml.
• Clean the Xcode build folder with: XCode Menu->Product-> (HoldingOptionKey)Clean build folder.
Android
The installerStore value on Android is the package name of the app store that installed the application, obtained via PackageManager.getInstallSourceInfo() (Android 11+) or PackageManager.getInstallerPackageName() (older versions).
installerStore | |
| Google Play Store | com.android.vending |
| Amazon Appstore | com.amazon.venezia |
| Samsung Galaxy Store | com.sec.android.app.samsungapps |
| Huawei AppGallery | com.huawei.appmarket |
| Xiaomi GetApps | com.xiaomi.mipicks |
| OPPO App Market | com.oppo.market |
| VIVO App Store | com.vivo.appstore |
| Manual/ADB install | null |
Some stores may not properly implement the installer package name API, which could result in null being returned even for store installations.
Web
The package_info_plus package uses the version.json file that it is generated in the build process, and it tries to locate the version.json using three methods:
1. Using the provided baseUrlin thefromPlatform()method.2. Checking the configured assetsfolder in the Flutter web configuration.3. Checking the path where the application is installed.
If we provide the optional custom baseUrl parameter, it will be used as the first option where to search.
await PackageInfo.fromPlatform(baseUrl: 'https://cdn.domain.com/with/some/path/');With this, the package will try to search the file in https://cdn.domain.com/with/some/path/version.json.
The second option where it will search is the assetBase parameter that we can pass to the Flutter Web Engine when we initialize it.
_flutter.loader.loadEntrypoint({ onEntrypointLoaded: async function(engineInitializer) { let appRunner = await engineInitializer.initializeEngine({ assetBase: "https://cdn.domain.com/with/some/path/" }); appRunner.runApp(); }});Finally if none of the previous locations return the version.json file, the package will use the browser window base URL to resolve its location.
It could be possible that the plugin cannot access the version.json file because the server is preventing it because of CORS permission.
static Future<PackageInfo> fromPlatform({String? baseUrl}) async { if (_fromPlatform != null) {return _fromPlatform!; } final platformData = await PackageInfoPlatform.instance.getAll( baseUrl: baseUrl, ); _fromPlatform = PackageInfo( appName: platformData.appName, packageName: platformData.packageName, version: platformData.version, buildNumber: platformData.buildNumber, buildSignature: platformData.buildSignature, installerStore: platformData.installerStore, installTime: platformData.installTime, updateTime: platformData.updateTime, ); return _fromPlatform!;}
夜雨聆风