How to upgrade Dart and Flutter

Flutter Jul 20, 2023

Upgrading Dart and Flutter is essential for better performance and new features in your Flutter projects. This post will show you how to easily upgrade both to their latest versions. We will also cover updating your project's requirements. By the end, you will be ready to make the most of the newest Dart and Flutter releases.

Upgrading

Upgrading Flutter and Dart to their latest versions is a simple process. Just execute the command flutter upgrade, and it will automatically update both Flutter and Dart to the latest versions on your system.

flutter_executing_flutter_upgrade

After the upgrade, you can verify the installed versions by running flutter --version, which will display the installed Flutter and Dart versions.

flutter_executing_flutter_version

In this case, we installed Flutter version 3.10.6 and Dart version 3.0.6.

Version restrictions

In the project itself, we still need to make some changes. In our project's pubspec.yaml file, we need to set the allowed versions for Flutter and Dart. This is done within the environment entry, as shown below:

environment:
  flutter: '>=2.5.0'
  sdk: '>=2.19.5 <3.0.0'

In this code snippet, the Dart version is set to be equal to or higher than 2.19.5 using the >= operator and lower than 3.0.0 using the < operator. Similarly, the Flutter version is restricted to be equal to or higher than 2.5.0 using the >= operator.

To allow newer versions, we can modify the code as follows:

environment:
  flutter: '>=3.0.0 <4.0.0'
  sdk: '>=3.0.0 <4.0.0'

However, since we have already installed Flutter version 3.10.6 and Dart version 3.0.6, we can consider them as the lowest requirement for our project:

environment:
  flutter: '>=3.10.6 <4.0.0'
  sdk: '>=3.0.6 <4.0.0'

And we can simplify this even further by using the caret ^ operator:

environment:
  flutter: '^3.10.6'
  sdk: '^3.0.6'

The caret operator also ensures that Dart and Flutter versions are equal to or higher than the given versions and lower than the next major version which in this case is 4.0.0.

Testing

Now, since we have switched from Dart 2 to Dart 3, let us test if Dart 3 is usable in our project. We will use a new feature available in Dart 3 called switch expression:

void main() {
  String test(int number) {
    return switch (number) {
      1 => 'test',
      _ => 'test'
    };
  }

  print(test(1));
}

In this code snippet, we created a simple switch expression to test out if it is possible to use Dart 3 features. By running this code within your project, you can confirm whether Dart 3 is compatible.

If you want to learn more about switch expressions, you can explore my related post:

Difference between Switch Statements and Expressions in Dart
Dart 3 brings switch expressions, which greatly simplify switch statements. They are shorter and easier to read because they remove the need for case and break keywords. Switch expressions offer a more straightforward and concise way to achieve the same functionality,

Packages

Now that our project supports the latest versions of Flutter and Dart, it is essential to verify that our packages are also up-to-date and compatible. If this is not the case our project will start throwing errors.

To address this, I have created a dedicated post that explains how to upgrade outdated packages in Flutter.

How to upgrade outdated packages in Flutter
Upgrading outdated packages is an essential task in Flutter development. It ensures that your app benefits from bug fixes, performance improvements, and new features provided by the package developers. In this post, we will go through

Conclusion

In this post, you learned how to upgrade Flutter and Dart to their latest versions. We explored adjusting version restrictions in our projects and keeping everything up-to-date for new features and better code. If you want to address any package-related issues, check out the separate post I suggested. By following these steps, your projects stay current and benefit from the latest improvements.

Tags