How to upgrade outdated packages in Flutter

Flutter May 21, 2023

Upgrading outdated packages is an essential task in Flutter development. It ensures that your application benefits from bug fixes, performance improvements, and new features provided by the package developers. In this post, we will go through the steps of identifying outdated packages, upgrading them using automated methods, and manually upgrading packages when necessary.

1. Inspect the pubspec.yaml

In the pubspec.yaml I have the following packages:

name: webfeed
version: 0.7.0
description: webfeed is a dart package for parsing RSS and Atom feeds. Media, DublinCore, iTunes, Syndication namespaces are also supported.
homepage: https://github.com/witochandra/webfeed

environment:
  sdk: '>=2.12.0 <3.0.0'
  
dependencies:
  xml: ^5.0.2
  intl: ^0.17.0
  
dev_dependencies:
  test: ^1.3.0
  http: ^0.13.0

2. Check outdated packages

Before upgrading our packages, we first need to check which ones are outdated. We can do that by executing the flutter pub outdated command.

flutter_package_upgrade_1

You can see that 2 packages are outdated.

3. Upgrade the outdated packages

Now we can upgrade them, as you might have noticed in the previous screenshot, we can do that by executing the following command: flutter pub upgrade --major-versions.

Be aware that upgrading major versions can bring breaking changes. If you do not want to upgrade to major versions, execute flutter pub upgrade instead.

flutter_package_upgrade_2

As you can see 2 packages were changed by executing flutter pub upgrade --major-versions.

4. Upgrade packages manually

As you might have noticed, when executing flutter pub outdated it said that my dev_depencies are up-to-date. However, when we check the http package, you can see that it has newer versions available.

flutter_package_upgrade_3-1

If you are running into the same problem, you can change the versions of the packages manually. Of course, this requires more work because you have to check the new available versions yourself.

There are 2 ways to upgrade the packages manually.

4.1 Using pubspec.yaml

Look up the version for every package that did not get detected by flutter pub outdated command and change it in the pubspec.yaml. I changed the versions for both the http and the test package on lines 14 and 15.

name: webfeed
version: 0.7.0
description: webfeed is a dart package for parsing RSS and Atom feeds. Media, DublinCore, iTunes, Syndication namespaces are also supported.
homepage: https://github.com/witochandra/webfeed

environment:
  sdk: '>=2.12.0 <3.0.0'
  
dependencies:
  xml: ^6.3.0
  intl: ^0.18.1
  
dev_dependencies:
  test: ^1.24.3
  http: ^0.13.6

After you made the changes, you need to run flutter pub get to make sure that your changes are applied.

flutter_package_upgrade_4

4.2 Deleting the package and reinstalling it again.

By deleting and reinstalling, you will automatically install the package with the latest version.

Delete the package by executing the flutter pub remove http command where http should be the package name. After that reinstall the package by executing either flutter pub add http or flutter pub add http --dev. The --dev flag will place the package underneath the dev_depencies.

flutter_package_upgrade_5

5. Test your application

At last, because we upgraded all our packages, we need to make sure that our application still works as intended. We can do this by running our tests by executing the flutter test command.

flutter_package_upgrade_6

If needed, you can also go through your application manually to make sure that your application does not have any issues.

6. Conclusion

Upgrading Flutter packages is a simple process that requires a few commands. However, it is worth noting that automatic upgrades may not cover all packages. In such scenarios, manual upgrades can be done by modifying the pubspec.yaml file or by removing and reinstalling the packages. By staying up to date with package updates, you can take advantage of the latest improvements, ultimately enhancing the overall quality of your Flutter application.

Tags