Skip to content

Instantly share code, notes, and snippets.

@lcw99
Created November 28, 2024 06:38
Show Gist options
  • Save lcw99/4b5d74b8920d51358b61e32b19baf7a7 to your computer and use it in GitHub Desktop.
Save lcw99/4b5d74b8920d51358b61e32b19baf7a7 to your computer and use it in GitHub Desktop.

The Gotcha with package_info_plus in Flutter Web Applications

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.

Understanding the Issue

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:

  1. During the build process, Flutter web generates a version.json file
  2. This file is placed in your web distribution folder
  3. When package_info_plus needs to retrieve version information, it makes an HTTPS request to fetch this file from your web server

The Real Problem

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

Alternative Approaches

Instead of relying on package_info_plus for web version checking, consider these alternatives:

  1. Embed Version Information Directly:
class AppConfig {
  static const String buildNumber = '42';  // Updated during build
}
  1. Use Build-Time Constants:
// main.dart
const String BUILD_NUMBER = String.fromEnvironment('BUILD_NUMBER');

Best Practices

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

Conclusion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment