How To Upgrade Your Old Flutter Projects (include migration to null safety)

Alperen Yalçn
3 min readJan 9, 2022

Hi, if you have unfinished Flutter projects that started with enthusiasm and not touched for months you might want to update these projects:) . First i will start with regular update and than continue with ‘how to upgrade to null safety’.

Standart Upgrade

First we need to run the command below

flutter upgrade

After this command you will get the similar output below

Upgrading Flutter to {latest version} from {current version}
Checking Dart SDK version…
Downloading Dart SDK from Flutter engine 890a5fca2e34db413be624fc83aeea8e61d42ce6…
Expanding downloaded archive…
Building flutter tool…
Running pub upgrade…
Upgrading engine…
Downloading …]
Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 77d935af4d (3 weeks ago) • 2021–12–16 08:37:33 -0800
Engine • revision 890a5fca2e
Tools • Dart 2.15.1

And now you upgraded your Flutter and Dart sdk’s. Now there is only one thing left. It is upgrading the packages you have in pubspec.yaml. For this we need to run the command below

flutter pub upgrade

Also you can check your outdated packages via this command below

flutter pub outdated

Now your project is updated and ready to work.

Migrating to null safety

Well, if you created your project six months before this article was written probably you are wondering how to upgrade your project to null safety. Luckily you are in the right article:)

First you need to make sure that your all packages has support for null safety. We can see this with the command below.

dart pub outdated --mode=null-safety

İf the output says ‘All your dependencies declare support for null-safety.’ ,then you can start migrating. If not you need to decide to use that package or not.

Here is an output for the command above. The green checkmarked packages supports null safety and in this example all packages support.

After checked packages null safety support we can update packages with the commands below

dart pub upgrade --null-safety

dart pub get

After these commands probably you will get a lot of syntax error which easy to solve because most of errors can be fixed by simply adding ?, ! ,required but if you don’t want to deal with you can use dart migration tool.

Dart migration tool helps you to fix these syntax errors. We can run this tool via the command below.

dart migrate

And if your all packages is upgraded to null safety version you will get the output below. If you wont see the output below it is because of a package not upgraded to null safety version.

Analyzing project…
[ — — — — — — — — — — — — — — — — -\]
No analysis issues found.
Generating migration suggestions…
[ — — — — — — — — — — — — — — — — -\]
Compiling instrumentation information…
[ — — — — — — — — — — — — — — — — -\]
View the migration suggestions by visiting:
http://127.0.0.1:53303/C:/Users/{path}Use this interactive web view to review, improve, or apply the results.
When finished with the preview, hit ctrl-c to terminate this process.

Now you need to click the link in the output so you can see the changes in the project. After clicked the link you will see the similar image below.

İf your project is too big you might not want to migrate all project at once so you can choose the files you want to migrate. After you ready you can click to ‘APPLY MIGRATION’ button.

After doing this update your packages again using the command below.

dart pub get

You just migrated your project to null safety. Now you are ready to create the next unicorn. See you next article 👊.

--

--