19 December 2024 Leave a comment Tech-Help
When developing a Flutter application, especially during the beta phase, it’s essential to display the build and version number to users. This guide provides a comprehensive solution to programmatically fetch these details within your Flutter app.
Using the package_info_plus
Plugin
The package_info_plus
plugin is the most efficient way to retrieve the app’s build and version number. This plugin is the updated version of the deprecated package_info
plugin and is maintained by the Flutter Community Plus Plugins. Here’s how to implement it:
Step-by-Step Implementation
-
Add the Dependency:
Include the
package_info_plus
dependency in yourpubspec.yaml
file:dependencies: package_info_plus: ^1.0.6
-
Import the Package:
Add the following import statement in your Dart file:
import 'package:package_info_plus/package_info_plus.dart';
-
Retrieve Version Information:
If your method is marked as
async
, you can use the following code to fetch the app details:PackageInfo packageInfo = await PackageInfo.fromPlatform(); String appName = packageInfo.appName; String packageName = packageInfo.packageName; String version = packageInfo.version; String buildNumber = packageInfo.buildNumber;
If you prefer not to use
await/async
, you can use thethen
method:PackageInfo.fromPlatform().then((PackageInfo packageInfo) { String appName = packageInfo.appName; String packageName = packageInfo.packageName; String version = packageInfo.version; String buildNumber = packageInfo.buildNumber; });
Alternative Approaches
While the package_info_plus
plugin is highly recommended, there are alternative methods like using a FutureBuilder widget to directly integrate version fetching within the widget tree, or employing Dart scripts for version extraction from pubspec.yaml
. However, these alternatives may not offer the same level of convenience or reliability.
Enhancing Your Testing with Repeato
As you develop and test your Flutter application, consider using Repeato, a no-code test automation tool. Repeato allows you to create, run, and maintain automated tests for your iOS and Android apps efficiently. Its computer vision and AI capabilities ensure that your tests are not only fast to edit and run but also highly reliable. This can be particularly beneficial when verifying app versioning and functionality across different platforms.