Related articles:

  • Migrating to NULL Safety

  • Chinese translation of official documents – migration to air security

  • The Flutter upgrade 2.0 pit filling guide takes you to take off in place


1. Create a branch namedflutter2_null-safetyAnd switch to the new branch.
2. Used in the projectfvm, the implementation offvm use stable, the use ofFlutterThe lateststableBranch.
3. Performflutter doctor
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.1 20G80 darwin-x64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Connected device (2 available)

• No issues found!

Copy the code
4. Check thedartVersion: Whether to set2.12Or higher
$ dart --version
fvm: running Dart from Flutter "stable"

Dart SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "macos_x64"
Copy the code
5. Check the migration status of all dependencies
$ dart pub outdated --mode=null-safetyfvm: Those who qualify can go onto university or university. Running Dart from Flutter "stable" Showing dependencies that are currently not opting in to null-safety Versions without null safety support. [opting] indicates versions of the Package Name Current Upgradable for opting to null safety Resolvable Latest direct dependencies: Those who qualify onto university can go onto university 0.2.0 (git) - - qualify 0.17.4 (git) - audioplayers who qualify onto university 0.17.4 (git) - azlistview qualify 0.1.2 (git) - Those who qualify can go onto university who qualify can qualify 2.0.2 - - qualify 3.0.1 cached_network_image qualify 2.5.1 - - qualify 3.1.0 Those who qualify can go onto university who qualify can go onto university. Those who qualify can go onto university who qualify can go onto university. Those who qualify can go onto university ✓ 2.0.2 dio ✗ 3.0.10 - ✓ 4.0.0 event_bus ✗ 1.1.1 - ✓ 2.0.0 exifdart ✗ 0.7.0 + 3 - ✓ 0.8.0 - dev. 2 fdottedline ✗ 1.0.1... Those who qualify can go onto university 1.0.1 flutter_app_Badger those who qualify can go onto university 1.1.2 - - qualify 1.2.0... . . dev_dependencies: Build_runner those who qualify can qualify 1.11.1 -- qualify 2.0.6 No resolution was found. Try running 'dart pub upgrade --null-safety --dry-run' to explore why.Copy the code
6. Depends on the migration priority

A depends on B, and B depends on C. The migration sequence is C > B > A.

7. Analyze whether the locally imported library meets the migration conditions

Several custom plugin libraries have created null-safety branches to support migration. ② The other three libraries have also found a replacement for the null-safety library. (3) Use git to import local plugins and create null-safety branch to support migration.

All of the above plug-ins are guaranteed to be migrated.

If the plugin has not been migrated, use the method below (⑧) to migrate the plugin first, and then use the null-safety branch.

  talkingdata_appanalytics_plugin:
    git:
      url: http:// * * * * * * * /* * * * * * */app_analytics_sdk_flutter.git ref: null-safetyCopy the code
8. Start the migration
1.fvm use stable
2.dart --versionMake sure you are using the latest version of DART
(3) to rundart pub outdated --mode=null-safetyTo ensure that all dependencies are up to date and empty secure.
$ dart pub outdated --mode=null-safetyfvm: Those who qualify can go onto university or university. Running Dart from Flutter "stable" Showing dependencies that are currently not opting in to null-safety Opting without null safety support. [opting] indicates the option to opt for null safety. All your dependencies declare support for null-safety.Copy the code

If the dependency has not been updated to the latest version, such as:

$ dart pub outdated --mode=null-safetyfvm: Those who qualify can go onto university or university. Running Dart from Flutter "stable" Showing dependencies that are currently not opting in to null-safety Versions without null safety support. [opting] indicates versions of the Package Name Current Upgradable for opting to null safety Resolvable Latest direct dependencies: Those who qualify can go onto path_provider university 1.6.22 - ✓2.0.2 ✓2.0.2 uuid university 2.2.2 - ✓3.0.4 ✓3.0.4 dev_dependencies: Those who qualify can go onto HTTP university 0.12.2-✓ 0.13.3 ✓0.13.3 No resolution was found. Try running 'dart pub upgrade --null-safety --dry-run' to explore why. 3 dependencies are constrained to versions that are older than a resolvable version. To update these dependencies, edit pubspec.yaml, or run `dart pub upgrade --null-safety`.Copy the code

Run dart PUB Upgrade –null-safety to upgrade the dependency to the latest version that supports air safety. Note: This command will change your pubspec.yaml file.

(4) in includepubspec.yamlDirectory in which to executedart migrateCommand to start the migration tool.
$ dart migrate 
fvm: running Dart from Flutter "stable"

Migrating /Users/yuanzhiying/Desktop/app_analytics_sdk_flutter

See https://dart.dev/go/null-safety-migration for a migration guide.

Note: more than one project found; migrating the top-level project.

Analyzing project...
No analysis issues found.

Generating migration suggestions...
Compiling instrumentation information...
View the migration suggestions by visiting:

  http://127.0.0.1:52996/Users/yuanzhiying/Desktop/app_analytics_sdk_flutter?authToken=zbeBO82KY54%3D

Use this interactive web view to review, improve, or apply the results.
When finished with the preview, hit ctrl-c to terminate this process.

If you make edits outside of the web view (in your IDE), use the 'Rerun from
sources' action.
Copy the code
(5) accesshttp://127.0.0.1:52996/Users/yuanzhiying/Desktop/app_analytics_sdk_flutter?authToken=zbeBO82KY54%3DThe browser opens and handles the code as prompted or as the case may be.

The default processing method is recommended by the tool. Click the code to see the detailed instructions in Proposed Edits on the right. In the Edit Details pane of the migration tool, you can Add /*? */ hint /*! */ Hint button to add the hint tag.

When you press these buttons, the corresponding tag is immediately added to the code and cannot be undone. If you want to remove the tag, you can do so using the code editor as usual.

Even if the migration tool is running, you can use the editor to add a prompt tag. You cannot use the new features of empty security because your code has not migrated to empty security. But you can make changes that have nothing to do with air security, such as refactoring.

When you are finished editing, click Rerun from Sources to make the changes.

⑥ Only some files are migrated

While we’d like you to do this all at once, it’s not easy for a large application or package. If you want to migrate only some files, please uncheck the green check box in front of the files that are not being migrated. Dart 2.9 [Version Comments][Version Comment] will be added to these files later when migration changes are applied, leaving everything else unchanged.

For more on progressive migration air security, read Unsound air security.

⑦ After the processing, the execution againdart migrate
$ dart migrate
fvm: running Dart from Flutter "stable"

Migrating /Users/yuanzhiying/Desktop/app_analytics_sdk_flutter

See https://dart.dev/go/null-safety-migration for a migration guide.

Note: more than one project found; migrating the top-level project.

Analyzing project...
All sources appear to be already migrated.  Nothing to do.
Copy the code
9. The migration is complete

Unsound air safety

A Dart program can contain both libraries that are already empty security and libraries that have not been migrated to empty security. These mixed-mode programs run under unsound empty security.

In order to maintain compatibility with traditional library runtimes while providing robustness for robust empty security programs, the Dart tool provides support for two modes:

  • A mixed-mode program that runs with unsound empty security. This may occur at run timenullMisquoted, but only because of somenullValues and nullable types contaminate null-safe code in non-null-safe libraries.
  • When a program is fully migrated to empty security and all the libraries it depends on are migrated, it is running with sound empty security, with all the guarantees and compilation optimizations that come with health.
Gradually the migration

Because Dart supports mixed-mode empty security, you can migrate your libraries (usually a file) one by one while running programs and tests. We recommend that you migrate the lowest level libraries first — libraries that have no other packages imported. Then migrate dependencies that directly depend on the underlying libraries. Finally, migrate the libraries with the most dependencies. For example, suppose your lib/ SRC /util.dart imports other (empty safe) packages and core libraries, but it does not contain any reference to import ‘< local path >’. Then you should migrate util.dart first and then migrate files that depend on util.dart. If there are libraries that have circular references (for example, A references B, B references C, and C references A), it is recommended that they be migrated at the same time.

Using the Migration Tool

You can use the migration tool for progressive migration. If you need to exclude certain files or folders, check the green checkbox. In the screenshot below, all files in the bin folder have been excluded.

Every file that is not migrated will be commented for the 2.9 language version. You can run Dart Migrate again later to continue migrating. Files that have been migrated will appear as disabled checkboxes, and they cannot undo the migration changes.

Test or run a mixed version of the program

To test or run a mixed version of the code, you need to disable sound empty security. There are two ways to do this:

  • indartflutterCommand, join--no-sound-null-safetyThe flag disables. Such as:
$ dart --no-sound-null-safety run
$ flutter run --no-sound-null-safety
Copy the code
  • Or, set the language version of the program entry – includingmain()Function file — set to2.9. inFlutterIn the application, it’s usuallylib/main.dart. In a command line application, it’s usuallyBin / < name > package. The dart. You can also set ittestAs they also contain program entry. For example,
/ / @ dart = 2.9
import 'src/my_app.dart';

main() {
  / /...
}
Copy the code

These two circumvents are useful for incremental migration, but doing so means that you are not testing your code with empty security fully enabled. When you have completed the incremental migration, also remember to remigrate the test code to empty security.