Support Apple Silicon, Added default lint、 Better tools and new language features improve productivity .
This month, , We released Dart SDK 2.14 The official version of , The new version is designed through unique portability 、 Combine productivity and robustness to create the best platform for building applications . This time, , We are right. Apple Silicon Provides better support , And provides many productivity enhancing functions , For example, the standard used to catch errors when you write code through code style analysis lint Code rules 、 Faster release tools 、 Better cascading code format and some small language feature updates .
since Apple stay 2020 Release new at the end of the year Apple chip Since the processor chip , We are always committed to updating Dart SDK To add support for native runs on new processors . The required updates are already in dev The channel has been available for some time , Last month ,beta The channel also provides support , from Dart 2.14.1 Start , Can be in Dart stable Used in the channel . When you download One macOS Of SDK when , Make sure you choose ARM64 Options . Please note that , And Flutter SDK Tied up in Dart SDK There is no support for these improvements .
This support is included in Apple Running on the chip SDK and Dart virtual machine , Also supported in Apple Executable file compiled and run on chip ( Use dart compile command ). because Dart Command line tools use native tools Apple Chip support , So they start much faster .
Developers usually prefer to let the code follow a certain style . Many of these rules are not just style preferences ( As we all know tab Discussion with spaces ), It also covers coding styles that can lead to errors or introduce errors . for example ,Dart Style guidelines require curly braces for all control flow structures , Such as if-else sentence . This prevents the classic 「 In the air else」 problem , That is, there are multiple nested if-else There is ambiguity in the statement . Another example is type inference , Although when declaring variables with initial values , There is no problem using type inference , But in Declare uninitialized variables It's important to specify the type manually , This ensures type safety .
Maintain a good code style, of course, you can also choose some form of manual review , That is what we are familiar with code review. however , Run static analysis when writing code to enforce rules , Usually more effective .
stay Dart in , This static analysis is highly Configurable , We have Hundreds of style rules ( Also known as lints). With so many options , It may cause us not to know which rule to enable .Dart The team maintained one Dart Style guide , It describes what we think is written and designed Dart Code best practices , We haven't provided a formal official style guide before linter The rules .
Many developers , Also include pub.dev Website score Engines are using a called pedantic Of lint The rules . However ,pedantic Originated inside Google Dart Style guide , For historical reasons , It's with the general Dart The style guide is not the same . therefore ,Flutter The framework has never been used pedantic Rule set , It has its own set of normative rules .
This may sound a little messy , But it does . But in this release , We are pleased to announce , We now have a new set of lint Collection to implement the style guide , also Dart and Flutter SDK Updated to use these rulesets for new projects by default . These rule sets include ：
- package:lints/core.yaml: Dart The main rules in the style guide , We think that all Dart Code should follow this rule .pub.dev Scoring engine updated , Use these rules instead of pedantic.
- package:lints/recommended.yaml: Core rules , Plus additional recommendation rules . This set of rules is recommended for all general Dart Code .
- package:flutter_lints/flutter.yaml: Core and recommended rules , Plus the extra Flutter Special recommendation rules . This set of rules is recommended for all Flutter Code .
If you use... In your existing project pedantic, We strongly recommend that you upgrade to these new rule sets . from pedantic upgrade Just a few steps .
We are right. Dart Formatter formats cascade The way the code is optimized . before , Formatter can produce chaotic formatting in some cases . for example , In this case ,
doIt() How is it called ？
var result = errorState ? foo : bad..doIt();
It seems that it will always be
bad call , But the actual superior applies to the whole ? expression , So the cascade is called on the result of the expression , Not just in
false Clause , The new formatter will make this clear ：
var result = errorState ? foo : bad ..doIt();
Other changes involve formatting rows that have multiple cascades , And how far the cascade is generally indented . We also greatly improve the formatting speed of code containing cascading ; For Protocol buffer Generated Dart In the code , We see formatting speed
All details about the problem , See and track this Pull Request.
at present , When you Release One package To pub.dev Community warehouse ,
pub All files in the folder will be captured , But the hidden files will be skipped ( Those with points
. Opening file ) and
.gitignore The documents listed in . Some developers need to be able to control which files are in
.gitignore Out of the list of . for example , You may be in a
tool/ There are some internal development tools in the folder , You use it to protect your package, But these tools and use you package It doesn't matter who you are .
Dart 2.14 Updated in the
pub The command supports new
.pubignore file , You can list what you don't want to upload to pub.dev The file of . This file is used with
.gitignore File format is the same . Details , see also package Publish documents .
pub Perhaps most commonly used to manage code dependencies , But it also has a second important use ： Provide powerful tool support . One example is Dart Testing tools , adopt
dart test Command to use . This command is actually just
pub run test:test A wrapper for the command , It runs package:test Test entry in . Before calling this entry ,
pub First compile it into native code that can run faster .
stay Dart 2.14 Before , Yes
pubspec Any changes to ( Include and
package:test Irrelevant changes ) Will invalidate this test build , And you'll see a bunch of this output , It includes 「 Precompiled executable 」：
$ dart test Precompiling executable... (11.6s) Precompiled test:test. 00:01 +1: All tests passed!
stay Dart 2.14 in ,
pub Become smarter about when to cancel the build step , Therefore, only when the version changes will it be built . Besides , We have improved the way we use parallelization to perform the build steps , Therefore, the step itself will be completed faster . In some of our tests package On , It only took us half the time to see it .
Dart 2.14 It also includes some small language features . This time, , We focus on more specific improvements , These improvements may be just minor features , But it can implement more professional use cases that were not supported before .
First , We added a new Triple shift Operator (
>>>). This is similar to the existing shift operator (
>>), But the
>> Perform arithmetic shift ,
>>> Perform a logical or unsigned shift , Whether the shifted number is positive or negative , The zero bit will be moved to the most significant bit .
We also removed the old restrictions on type parameters , This restriction does not allow the use of generic function types as type parameters . All of the following is in 2.14 It was invalid before , But now it's allowed ：
late List<T Function<T>(T)> idFunctions; var callback = [<T>(T value) => value]; late S Function<S extends T Function<T>(T)>(S) f;
Last , We made a small adjustment to the annotation type ( image @Deprecated This annotation is in Dart Code is often used to capture metadata ). Previously, annotations could not pass type parameters , So it's like
@TypeHelper<int>(42, "The meaning") Such code is not allowed . Now this restriction has been removed .
We are right about the core Dart package And the code base , Include ：
dart:core: towards Object Class adds static methods
hashAllUnordered. These can be used to combine hash codes of multiple objects in a consistent manner (hashAll Example );
dart:core: This machine DateTime Class can now better handle local time , Instead of changing daylight saving time to the nearest hour —— For example, Lord Howe Island, Australia , It has 30 Minute offset ;
- package:ffi: Added to use arena Allocator management memory support ( Example ).Arenas It's a kind of Area based memory management form , Once you exit arena/region Resources will be automatically released ;
- package:ffigen: Now support from C Type definition generation Dart The type definition .
Dart 2.14 It also includes some smaller 、 already Announced in advance Disruptive updates (breaking changes). It is expected that these changes will only affect some special use cases , These destructive updates are as follows ：
#46545: Cancel right ECMAScript5 Support for
All modern browsers Have supported the latest ECMAScript edition , So two years ago we Announced A plan , No longer supported ECMAScript 5 (ES5). This enables us to take advantage of the latest ECMAScript Improvement , Generate smaller output . stay Dart 2.14 in , The work has been completed ,Dart Web The compiler no longer supports ES5 了 , therefore , Older browsers ( for example IE11 ) Will no longer support .
#46100: Abandoning stagehand、dartfmt and dart2native
stay 2020 year 10 Of the month Dart 2.10 In a blog post , We announced that all Dart CLI Developer tools are grouped into a single group
dart Command tool ( Be similar to flutter Command tool ) The job of . As part of this evolution ,Dart 2.14 Abandoned the previous
dart2native command , And stopped
stagehand. These tools are in A unified dart Command tool There are equivalent alternatives .
#45451: Abandoning VM Native extensions
We have abandoned Dart VM Native extension of , This is what we do Dart Code calls the old mechanism of native code .Dart FFI ( External function interface ) Is our current mechanism for this use case , We are actively Development It makes it more powerful and easy to use .
We are 3 In the Dart 2.12 The version introduces a sound empty security . Air safety is Dart The latest major productivity features , Designed to help you avoid null errors , This is a kind of error that is usually difficult to find .
Since our last update , We see the existing package And application migration has made great progress , To achieve empty security Advantages of sound inspection . about pub.dev Upper package, front 250 Named package in 100% Have supported empty security , front 1000 In the name 94% All support . This means that more developers can use full Sound air safety To run their application . Analysis and display ,56% Of
flutter run Commands are executed in a completely sound manner . Thanks to all the developers in the ecosystem , Thank you for your migration !
Enhanced... Including the above changes Dart SDK Already available in Dart 2.14.1 and Flutter 2.5 SDK Use in . We hope you will like these new improvements and features .
thank Dart Community
in addition , We would like to take this opportunity to Dart Community thanks . Through some recent updates to the programming language survey , You can see Dart The momentum is strong . Respected RedMonk In the rankings Referred to the "Dart A significant increase in ", For the first time Dart Before inclusion 20 name .StackOverflow Of 2021 Comprehensive survey of developers in Equally gratifying . As report goes ,Dart Is the seventh favorite programming language for developers . We are really happy to see Dart The platform has sustained growth and development momentum .
thank flutter.cn Community members (@AlexV525、@Vadaski、@MeandNi) as well as Lynn Review and contribution to this paper .