When developing Flutter web applications, many developers use the package_info_plus
package to retrieve app version information. However, there's a crucial gotcha that you need to be aware of, especially when implementing version checking or update mechanisms.
The problem lies in how package_info_plus
works specifically in web environments. Unlike mobile platforms where version information is embedded in the app bundle, Flutter web handles this differently:
- During the build process, Flutter web generates a
version.json
file - This file is placed in your web distribution folder
- When
package_info_plus
needs to retrieve version information, it makes an HTTPS request to fetch this file from your web server
This mechanism creates a potential issue for version checking. Here's why:
// This might not work as expected in web
final packageInfo = await PackageInfo.fromPlatform();
final buildNumber = packageInfo.buildNumber;
When your app makes this call, it's actually performing a network request to fetch the latest version.json
from your server. This means:
- If you deploy a new version but a user is still running an old version, they'll still get the new version number
- This makes it impossible to reliably detect if the user is running an outdated version of your app
- Version comparison logic might fail since the reported version will always be the latest one on the server
Instead of relying on package_info_plus
for web version checking, consider these alternatives:
- Embed Version Information Directly:
class AppConfig {
static const String buildNumber = '42'; // Updated during build
}
- Use Build-Time Constants:
// main.dart
const String BUILD_NUMBER = String.fromEnvironment('BUILD_NUMBER');
To implement robust version checking in Flutter web:
- Always store a local build number constant in your application code
- Don't rely solely on
package_info_plus
for version comparison in web - Use consistent build number increments for reliable comparisons
While package_info_plus
is a valuable package for Flutter development, its web implementation has limitations that make it unsuitable for version checking. Understanding these limitations helps you design more robust update mechanisms for your Flutter web applications.
Remember: In web environments, the version information from package_info_plus
will always reflect the latest deployed version, not the version currently running in the user's browser.