31

I have an existing Flutter app that I built half a year ago. I checked on pubspec.lock, it has this line:

sdks:
  dart: ">=2.10.0-110 <2.11.0"
  flutter: ">=1.16.0 <2.0.0"

So I assume the app was built for Flutter v1.16.

  1. How do I upgrade this app to use the latest Flutter version? Running "flutter doctor" gives me [√] Flutter (Channel stable, 1.22.3, on Microsoft Windows [Version 10.0.19041.630], locale en-US), so my SDK is already updated to the latest version.

  2. Interestingly, when I create a new app from scratch, the pubspec.lock does not have any information about Flutter's version. The same section now contains only this: sdks: dart: ">=2.10.0-110 <2.11.0". In the future, how would I know which version was this app running?

I have tried running "flutter upgrade" within my app, but that was to upgrade the SDK, not the Flutter version of the app. So how do I upgrade my app to use the latest Flutter version? Or is it always built using the latest version of the SDK? Can't I target my app to build with a specific version of Flutter?

Hamed
  • 5,867
  • 4
  • 32
  • 56
user1491884
  • 589
  • 1
  • 5
  • 12

5 Answers5

47

The following steps requires dart 2.19 tool or lower because the dart migrate tool is removed in version 3:

To update the project to null safety follow these steps:

Side note: change the flutter version in pubsec.yaml, make new project and copy the following line:

sdk: ">=2.19.6 <3.0.0"

Then follow the steps:

  1. Run flutter upgrade in the terminal to upgrade Flutter
  2. Run dart migrate to run the dart migration tool.
  3. Solve all errors which the migration tool shows.
  4. Run flutter pub outdated --mode=null-safety to print all outdated packages.
  5. Run flutter pub upgrade --null-safety to upgrade all packages automatically.
  6. Check the code for errors and solve them (Very important).
  7. Run dart migrate again and it should now be successful. Follow the link to checkout the proposed changes.
  8. Press the "Apply Migration" button.
  9. Check the code for errors again and fix them.

Your project should be updated now. Referenced from this website.

Zacktamondo
  • 1,891
  • 3
  • 17
  • 33
  • 2
    Best Answer, because it's the simplest and easiest way described. – Paul Jul 25 '21 at 16:55
  • This did the trick for me. In my case I needed to upgrade manually android build tool: classpath 'com.android.tools.build:gradle:7.2.0' ...and gradle distributionUrl: distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip – Raphael Sá May 15 '23 at 17:34
8

Go ro the root directory of your flutter project and run flutter upgrade. This will upgrade both your existing flutter project and flutter SDK to latest version.

Run the command:

flutter upgrade

Additional for upgrading to latest stable version only

If you want to upgrade to latest version only then please first check your current flutter channel

Step 1: Check for Stable channel

flutter channel

You will get output like this:

  beta
  dev
  master
* stable

If output was not like this then please proceed to Step 2 or directly proceed to Step 3

Step 2: Switch to flutter stable channel

flutter channel stable

Now you will get output like this:

  beta
  dev
  master
* stable

Step 3: Run the below command to upgrade to latest stable version of flutter

flutter upgrade
Chinmay Mourya
  • 764
  • 5
  • 9
  • 9
    I understand this. But isn't this upgrade the SDK? How about the app itself? I come from the .NET background, where an application can target which version of the framework that it wants to build against. Does Flutter not work that way, does it always build against the SDK version? Can it be built using specific version? – user1491884 Nov 19 '20 at 04:42
  • 2
    @user1491884 I would like to know this as well. – Nirvan Nagar Mar 20 '21 at 15:28
  • 2
    I am having the same problem. I want to updated the app itself without touching the flutter SDK. I come from an Angular background where you can update the app's local Angular version while leaving the globally installed version untouched. – Frank May 30 '21 at 07:09
3

Inside your project root, run below command

flutter upgrade

This command will update the Flutter SDK locally installed on your workstation, wondering how does that make sense?

After you run, above command flutter upgrade the SDK is updated locally and whenever you build or run your flutter app now, it should always pick up the latest stable version (stable channel) installed locally.

to check run,

flutter channel

you should get something like below based on what version is installed locally on your workstation.

Flutter is already up to date on channel stable
Flutter 2.0.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d7946a68d (3 days ago) • 2021-03-18 17:24:33 -0700
Engine • revision 3459eb2436
Tools • Dart 2.12.2

You can always switch to a different channel with below command,

flutter channel dev
flutter upgrade

Answer to - the Flutter SDK locally installed on your workstation, wondering how does that make sense?

Open your pubspec.yml file and see below:

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  1. Where version: 1.0.0+1 is your flutter app version
  2. When you run command flutter run your app should pick up the environment as defined which is sdk: ">=2.1.0 <3.0.0"
  3. With all dependencies as defined under,
dependencies:
  flutter:
    sdk: flutter

where sdk: flutter states that the SDK should be pulled from your locally installed SDK version.

Recall, how you had installed the Flutter SDK first time, on your local workstation, as below MAC example, See all SDK releases

cd ~/development
unzip ~/Downloads/flutter_macos_2.0.3-stable.zip

later, Update your path, and run flutter doctor.

OR

git clone https://github.com/flutter/flutter.git -b stable

later, Update your path, and run flutter doctor.

Note: You may check your pubspec.lock file that may look like below,

sdks:
  dart: ">=2.12.0-0.0 <3.0.0"
  flutter: ">=1.16.0" <---- Use SDK greater than this installed locally
Sunil Kumar
  • 1,718
  • 16
  • 33
1

I will show you an easy way to migrate old code projects.

My recommendation is not to work on the existing old project. Instead, create a new project where you can turn your existing project code into new clean, tidy code at a time.

Now put a line of code on the existing old code project terminal as below.

$ flutter create -t <project-type> <new-project-path>

For example, your terminal code like,

PS C:\Users\habib\VScodeProject\git_ripository\simple-weather-old> flutter create -t app C:\Users\habib\VScodeProject\git_ripository\simple-weather-new

Done.
After that, Your new project will no more show any system problems, like gradle problems, android manifesto problems i.e.

If you want to see more about flutter migrating: [See this flutter doc]

Hamed
  • 5,867
  • 4
  • 32
  • 56
-1

if you upgrade your project, but still have problem. then please check your path spellin is ok or not!?

I also face this problem. after upgrading project it won't work for me. then i look to my code, there was miss path.

Azizur Rahaman
  • 181
  • 2
  • 5