Skip to content

How To Upgrade Your Mobile App to Flutter 2.0+

Technology

Aug 2, 2021 - 4 minute read

1837 Flutter Upgrade Blog 416X300
Arkadiusz Pachucy Team Leader Senior Software Developer

A lover of active leisure - escape rooms, laser tags, mountain walking. Since Engineer degree, Interested in mobile technology. The easy choice between platforms: Android

See all Arkadiusz's posts

2988 HC Digital Transformation 476X381

Share

As developers, sometimes we not only need to create new applications but also update the existing ones. In March 2021 Flutter released the newest version of its framework: 2.0.0. This article will discuss the steps that need to be taken when updating from version 1.22.6 to the latest one.

The Requirement

Our client asked us to modify the existing models for events and news as the ‘backend’ changes data models. What should be done in such a case? You should check if the project works on the older Flutter (1.22.6). Our project did run a compile command but after we tried to run it on an Android emulator, there was an issue related to one of the libraries: “Project cannot find a path to library …” . After checking the documentation and changelog, there was only one conclusion — the project needed to be migrated to the new Flutter version. Why? The previous versions of library were removed, and the newer one only supports Flutter 2.0+

What Changed in the Latest Version?

Quite a lot, Flutter matured over the last 9 months. Currently, there’s official support for more than 6 platforms.

This means that with the Flutter 2.0+ upgrade there are more widgets for developers to use and apps can be portable between these environments. More bugfixes, more stability and “The Billion Dollar Mistake” approach — nullability. For the layman we only need to add: “!” or “?” to the code and it should work. What’s more, Dart prepares a migration tool for nullability.

Update to Flutter 2.0+

This is our step by step action plan for updating Flutter to version 2.0+:

  • Run the Flutter tool for checking current libraries.
  • Update libraries to the nullability version.
  • Update code if there are breaking changes.
  • Run the tool for migration.
  • Run the app.
  • Make the changes the client asked for.

To check the current version of libraries the developers needed to run the command: dart pub outdated:

dart pub outdated command

Most of the libraries could be migrated to Flutter 2.0, but not all of them. In this case, the developers had two choices:

  • Drop the current library and try to rewrite the functionality.
  • Use flag: --no-sound-null-safety.

Since the project was time-boxed and there was a chance that other libraries would be migrated into the Flutter nullability, we chose the second approach. All libraries were updated to the newest version in this step.

Once this was done, we needed to adjust the current use of the auto_route library. Other libraries were ready to go after the version update — even if their major versions were changed.

It was time to run the dart migrate command and use tools that dart provides for migration. After more than 300s we received information about 608 (!) issues found.

In this project we used Freezed, auto_route and other packages as a code generator and the dart tool couldn’t handle the migration. Still, there was another option — do it manually with the flutter analyze command and IDE.

From this point, the main commands for developers were:

flutter clean

flutter packages get

flutter analyze

Fix next displayed issues

Having fixed all the issues in our code, we could re-generate code for freezed and auto_route using the command: flutter pub run --no-sound-null-safety build_runner watch -delete-config-outputs.

Here is a list of actions that need to be taken while updating the code:

Nullcheck — on named parameters in Flutter 1.22.6 we used the annotation @required — this needed to be changed to the required keyword (mostly models).

Nullcheck — add ‘?’ to the type that could be nullable (checks remain the same) — mostly in Widgets & Styles.

Nullcheck — late modifier for properties that are not initialised in the constructor.

Nullcheck — don’t forget to force non-null on a property using ‘!’ — after checking if the property is not null.

There is more emphasis on type as it can make a big difference. Previously, the method _buildPageAppBar could return Widget and now it needs to return PreferredSizeWidget, IDE couldn’t cast dynamic into the correct parameters type for lambdas (you need to add the type before the parameter name).

flutter widget

The function that handles user actions that are passed to widgets needs to be called: ()=> onPressed() (previously: onPressed).

After all the changes and fixes were applied, the flutter analyzer displayed on console: 0 issues found. It was time to run the app into the emulator. It was strange — this time the Android Studio displayed issues while trying to compile the code. Most of the issues were related to nullability and dynamic parameters, so the analyzer didn’t find every place where the code needed to be updated.
Thanks to the approach we chose, fixing the code took less time.

Flutter 2.0+ Migration — Conclusion

Please be patient with code and analyse it while asking yourself the question what would happen if this property could be nullable. Models from the network are an excellent starting point — depending on them the models in other layers could be null.

A migration tool is a great idea that could help a developer with an easy migration without too much overheating with code generation tools.

Don’t be scared if you see a lot of issues at the beginning — some of them will be fixed automatically by the code generator after fixing issues in your classes. Others, in most cases, will be smaller — adding ‘?’, ‘!’ or late would suffice.

You should only use libraries if you really need them — the maintenance could be really hard after some breaking changes. Remember, Flutter app development is relatively new, so there could be breaking changes in the platform.

2988 HC Digital Transformation 476X381
Arkadiusz Pachucy Team Leader Senior Software Developer

A lover of active leisure - escape rooms, laser tags, mountain walking. Since Engineer degree, Interested in mobile technology. The easy choice between platforms: Android

See all Arkadiusz's posts

Related posts

You might be also interested in

Contact

Start your project with Objectivity

CTA Pattern - Contact - Middle