Geek Culture
Published in

Geek Culture

My flutter 2.0 migration story : Still better story than twilight 🔮

Upgrade flutter and dart version to 2.0 and migrate the existing project to the new updated development environment

Disclaimer: Always make a complete backup before starting the migration process. It’s recommended that you have at least above-average skills in flutter or programming in general 👍, and if you have any unresolved dependencies. I wouldn’t recommend migrating your development environment unless you need to incorporate the features of flutter 2.0. Let’s start with the story.

The date was 27th July, The Flutter app… Needed an upgrade… 🤭

What does flutter 2.0 bring new to the table for a developer who enjoys and loves building native applications? The most important feature is Null safety, which strengthens Dart further by distinguishing nullable types from non-nullable types, which help to prevent Null crashes, which are very common causes of app crashes 😵‍💫. Google mobile ads and new Cupertino material components for iOS that look beautiful are my favourites.

First, you must upgrade your flutter and dart versions. You may do this by running flutter — — upgrade in your terminal. If this command does not work, it implies that modifications have been made to your flutter SDK, therefore you can force the upgrade by using the following instructions. After that, you can run flutter a doctor to verify the to-do list for checking your flutter installation.

Everything was rainbows and unicorns at beginning 🦄

Let’s go on to moving our application to the latest SDK version, but first make a backup of your flutter a project, as not all flutter projects can be migrated. We can use the command below for resolvable packages.

flutter pub outdated --mode=null-safety
Package checking

If you notice the dependencies with “x” at the extreme right, these are the ones that cannot be addressed, and if they give a functional possibility, you should wait until they are resolved. The ones with ticks are the jackpots; you may copy the versions and then paste them in your “pubspec.yml” file.

Find alternatives if packages are for Aesthetics

Now for the unsolved ones, because they did not provide functional prospects like state management, API processing, or session storage. I chose to drop a couple of them and look for alternatives on pub dev. Check pub dev for packages that do not resolve; you may still find many alternatives supporting null-safety. For example, instead of SplashScreen, I used flutter native splash: 1.2.0, which provides null safety. I’ll repeat the process for the remaining three packages, and we no longer have any package issues. Do not be alarmed if you notice a wave of errors; Everything will fall in place, hopefully 🥲.

Check that your dart version is at least 2.12 If not, use the command “dart- -version” to check your dart version. It is generally updated with your flutter package.

Let’s do this for science 🚀

You can skip to the next section no need fixing it manually. After upgrading, I changed the flutter SDK version to 2.12.0 3 in the pubspec file. The criteria for null safety are fairly straightforward. Let’s repair the null variables by adding late keywords and doing some manual labour for the purpose of science to observe what has changed. Example“@ from @required”

! to convert a nullable type to a non-nullable type, such as putting a null check in a snackbar, and many more rules Fortunately, I discovered a walkthrough, which I’ll provide in the resources section.

_scaffoldKey.currentState!.showSnackBar(snackbar);_formKey.currentState!.reset();}
Using the Late keyword for undefined placeholders

Now, after updating the variables and fixing 150+ nullable constraints, I still had a few problems, some of which are mentioned below, which I will manually correct because, you guessed it! FOR THE SAKE OF SCIENCE! 🧑‍🔬 How much time do you think it’ll take me to fix 23 errors? Update: It took an hour to fix them.

error • A value of type 'Color?' can't be assigned to a variable of type 'Color' at lib/configuration.dart:3:21 • (invalid_assignment)
error • The argument type 'Color?' can't be assigned to the parameter type 'Color' at lib/configuration.dart:6:13 • (argument_type_not_assignable)
error • The argument type 'Null' can't be assigned to the parameter type 'Key' at lib/.dart:595:27 • (argument_type_not_assignable)
error • The named parameter 'onInit' is required, but there's no corresponding argument at lib/.dart:46:16 • (missing_required_argument)
error • The named parameter 'key' is required, but there's no corresponding argument at lib/pages/.dart:27:14 • (missing_required_argument)
error • The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?' at lib/pages/cart.dart:324:15 • (argument_type_not_assignable)
error • The named parameter 'user' is required, but there's no corresponding argument at lib/pages/.dart:194:15 • (missing_required_argument)
error • The named parameter 'key' is required, but there's no corresponding argument at lib/pages/.dart:195:15 •
error • The named parameter 'user' is required, but there's no corresponding argument at lib/pages/.dart:216:15 • (missing_required_argument)
error • Undefined name 'user' at lib/redux/.dart:24:33 • (undefined_identifier)
error • The default 'List' constructor isn't available when null safety is enabled at lib/redux/.dart:156:42 • (default_list_constructor)
error • The named parameter 'store' is required, but there's no corresponding argument at test/widget_test.dart:16:29 • (missing_required_argument)

*Drum role* 🥁 successfully changed to null safety, but let’s see whether the app works again because I wanted to learn all the coversions on my own. Fingers crossed. The app did launched, but a few features were broken. Let’s give the dart migration tool a go, Now that we have complete are done with our experimentation.

You’ve lost your path… Your path start’s here. 🤗

I rolled back to the start kept the pub file from the migration one, removed the instances of unmet dependencies and did dart migrate.

Dart migrate success

At this time, I’d want to express my gratitude to the Flutter and Dart teams for their work in making the manual migration preview adequately informative 👏, with visual highlights that really show you the migration changes, which is awesome. I personally went over all of the proposed modifications and discovered them. I completed more than 85% of them successfully before clicking migrate. After clicking on migrate flutter does the changes automatically you dont have to worry 🤩.

Null safety changes proposed by the migration tool

Then I attempted the flutter run command. Yes, that didn’t seem to work as well, so I tried both simulators just to be sure, but no, so I did flutter clean and flutter pub get instead. No luck still stuck on blank screen

Flutter Migration blank screen result

Let’s see, when I performed it manually, just a few functions were broken, but the others worked fine. I was able to go through all of the panels, and the state management appeared to be operational. Since I conducted the manual migration and understood where things would most likely go wrong, I made the necessary modifications, and *Drum roll* 🥁 , it worked 🥳.

Worked 🥳

Everything from the functionality to the layouts may now take advantage of the new flutter V2 features. I’ll further be testing my app for any bugs and Once again Science saved us ! This was my flutter migration story. I’ll include a few resources that were really helpful to me and were pure bliss. Check out those great developers and give them your support. Happy coding ✨

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddhesh Shivdikar

Siddhesh Shivdikar

Hello! I'm Siddhesh , a software engineer based in Mumbai, India. I'm passionate about field of AI and Data Science