Flutter2 come

The biggest ape event of March 2021 will be Flutter Engage, according to Flutter2. I felt the beginning of a big front-end era led by Flutter from the Flutter2 debut video.

There are currently three links to Flutter2 on the website.

  1. Flutter 2 is here!
  2. Announcing Flutter 2
  3. Announcing stable web

Now you only need to write one piece of code to support almost any platform: Android, iOS, MacOS, Linux, Windows, Web and even embedded platforms.

From now on, we can concentrate on human, material and energy to do business well, and let Flutter take charge of cross-platform.

One of the most important new features in Flutter2 is to help you get your app right.

That’s Sound null Safety.

What is Sound Null Safety?

We can think of this as saying that your variables cannot contain NULL unless you explicitly state that they can be null.

With null safety, the DART compiler can detect null pointer errors at compile time.

Air security was introduced not to eliminate NULL, but to provide developers with tools to master the ability to control NULL.

What’s the use of air safety

The role of air safety has three levels:

  1. Editing tips: You can see syntax red lines in the IDE, and the compiler alerts you to syntax errors
  2. Early detection: Causes errors that would otherwise be found at run time to be found at compile time.
  3. Performance improvements: Gain size and speed improvements

What does air safety look like

Not empty safe Air safety

The top class of the entire Dart world is no longer Object but Object? The underlying class is no longer Null but Never

basis

By default, all variables are non-null

Air safety has “?”

The question mark after the variable type declaration:

// If you want to declare that a variable can be either an int or empty, just add a question mark to the variable's type declaration
int? aNullableInt = null;
Copy the code

The question mark after the function return value type:

// The return value type with a question mark indicates that it can return an int or a null value
int? getIndex() {
  return 1;
}

Copy the code

Air safety has…?

?? Operators are placed after nullable variables, followed by an operand.

Represents the value of the operand when the variable is null.

// Value = 0 when aNullableInt is null
int value = aNullableInt ?? 0;
Copy the code

Empty security inside the “!”

In a scenario, a nullable variable can be added if the developer can ensure that the variable is non-null!

For example,

int? getIndex() {
  return 1;
}

void main() {
  int? aNullableInt = null;

  List numbers = [1.2.3];

  aNullableInt = getIndex();

  // Make sure aNullableInt is non-null
  print('numbers[${aNullableInt}] :${numbers[aNullableInt!] }');
}

Copy the code

There is “Required” in air security

Required can only be added before the function named parameter type declaration.

This tells the compiler, “I’ll initialize this variable later.”

class MyHomePage extends StatelessWidget {
  // When the compiler misunderstands that a function argument is nullable, you can correct it with the required keyword
  MyHomePage({Key? key, required this.title}) : super(key: key);
  / /...
}
Copy the code

Empty security has “late” in it.

The late keyword is appended to the variable type declaration.

This tells the compiler, “I’ll initialize this variable later.”

class IntProvider {
  // You can be sure that a variable will be initialized to non-empty before it is used, but still get an error. You can mark late before the type of the variable
  late intaRealInt; IntProvider() { aRealInt = calculate(); }}Copy the code

List and Map are different

List<String? >?

type The list can be empty The item can be empty describe
List No No A non-empty list containing non-empty strings
List? Yes No A non-empty list of nullable strings
List<String? > No Yes Nullable list containing non-empty strings
List<String? >? Yes Yes A nullable list containing nullable strings

Map<String, int? >?

type The Map can be empty The item can be empty
Map<String, int> No No*
Map<String, int>? Yes No*
Map<String, int? > No Yes
Map<String, int? >? Yes Yes

Combat: How to migrate your application to air security

Migration routine:

1. Module sequence

Migrate the dependent module first and then the dependent module

I migrated Flutterame in this order:

  1. modules/utils
  2. modules/gallery
  3. The app

2. SanBanFu

For each module, we can use the following three axes:

1. Update the pub

dart pub get
Copy the code

2. View and modify the dependent libraries

Check out the dependency libraries

➜ dart pub outdated --mode= nulle-safety Showing dependencies that are currently not gamblein[university] Indicates versions without null safety support. [✓] Indicates versions for optinginto null safety. Package Name Current Upgradable Resolvable Latest direct dependencies: Cupertino_icons qualify 1.0.0 ✓1.0.2 ✓1.0.2 ✓1.0.2 1 upgradable dependency is locked (in pubspec.lock) to an older version.
To update it, use `dart pub upgrade`.
Copy the code

Modify the dependent library version

Open the pubspec.yaml file under the module to modify the library version under Dependencies

name: flutterame
#...
dependencies:
  flutter:
  #...
  # Change before Cupertino_icons: ^1.0.0
  cupertino_icons: ^ 1.0.2
#...

Copy the code

Update pub again

dart pub get
Copy the code

3. Perform automatic or manual migration

Automatic migration

dart migrate --apply-changes
Copy the code

When you submit your code, you can see what changes you have made and whether they make sense.

Manual migration

dart migrate
Copy the code

The output is as follows:

➜ dart migrate Migrating/Users/admin/Documents/flutter/flutterame/modules/utils to See https://dart.dev/go/null-safety-migrationfora migration guide. Analyzing project... [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ----------------------------------------------------|]No analysis issues found. Generating migration suggestions... [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -----------------------------------------------------] Compiling instrumentation information... [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -----------------------------------------------------] View the migration suggestions by visiting: http://127.0.0.1:49736/Users/admin/Documents/flutter/flutterame/modules/utils? authToken=xNh-pu7B9Fo%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

Then open “127.0.0.1:49736…” That link takes you to the following page

You can select the appropriate file, click on the non-null correlation judgment in the code (the blue tip), and you can see why the DART compiler made those judgments in the lower right corner. You can accept the judgment or change it.

Migration Results:

After the migration, I compared the difference of APP size before and after the migration.

3.8m libapp.so decreased by 124.7K

It can reduce errors and reduce volume. What are you waiting for? Move your project to air safety!

Reference:

Sound null safety

Migrating to null safety

Related video:

Dart’s null-type safety

Dart language robust air safety mechanisms

Migrating a Package to NULL Safety


Reprint please maintain the integrity of the article and indicate the source juejin.cn/post/693939…

If you find this article useful, please like ❤️

If you think this article might help others, just forward it

I will continue to pay attention to the new technology of Flutter and update related articles