Dart 2.12: Sound null safety and Dart FFI sent to stable channel

We present to you the translation of the article announcing the release of Dart 2.12. Yes, we know that we were very late with the publication: the original was released on March 3. However, Dart 2.12 is an important release and we decided to post it.





Dart 2.12 released. It contains stable versions of sound null safety and Dart FFI .





Null safety is our most recent large-scale feature. Its goal is to improve performance: to help you avoid null errors. The problems caused by this error are often hard to spot in your code. We talked about this in detail in the introductory video .





FFI is an interoperability mechanism that allows you to call existing C code. For example, access the Windows Win32 API . Dart 2.12 is already available.





Unique functionality of the platform on which Dart runs

sound null safety FFI, , Dart. . , web. .













: JS





,









hot reload













null-safe





Google





Dart :





  • . x86 ARM JavaScript web. Dart : , , . API, : .





  • . Dart hot reload, . , Dart , async/await - .





  • . null-safe Dart . , . , , , Google Ads Google Assistant, .





Sound null safety . Dart FFI C. C , .





Sound null safety

Sound null safety โ€“ Dart , Dart 2.0 . Null safety . null error , , , . Sound null safety .





Non-nullable :

null safety , , null, , null.





Flutter. Flutter null error: The method '>=' was called on null



. :





final int major = version?.major;

final int minor = version?.minor;

if (globals.platform.isMacOS) {

 // plugin path of Android Studio changed after version 4.1.

 if (major >= 4 && minor >= 1) {

 ...
      
      



final int major = version?.major; final int minor = version?.minor; if (globals.platform.isMacOS) {  // plugin path of Android Studio changed after version 4.1.  if (major >= 4 && minor >= 1) {  ... ? version



null, major



, minor



. , , , , , Flutter. null safety . , DartPad.





Screenshot with the result of the analysis in the IDE
IDE

. null safety Google, . , null safety. :





  • , null , null. , protobuf, , , null. : null.





  • Google Pay Flutter, Flutter State



    Widget.



    null safety null ; null safety , null , .





  • Flutter , Flutter , scene



    Window.render



    () null



    . null safety โ€” mark Scene as non-nullable, , null.





non-nullable

null safety , non-nullable:





// In null-safe Dart, none of these can ever be null.

var i = 42; // Inferred to be an int.

String name = getFileName();

final b = Foo();
      
      



, null, , ? :





// aNullableInt can hold either an integer or null.

int? aNullableInt = null;
      
      



Null safety : nullable . , null, Dart nullable non-nullable:





int definitelyInt(int? aNullableInt) {

 if (aNullableInt == null) {

   return 0;

 }

 // aNullableInt has now promoted to a non-null int.

 return aNullableInt;

}
      
      



, required



. , required



( API Flutter) , :





null safety

Null safety โ€“ . , : , . 





Null safety โ€“ : Dart 2.12 null safety. , null safety, , null safety .





null safety,





. null . - , . .





, dart create



flutter create,



sound null safety. , , . null safety dart migrate



.





null safety Dart

- sound null safety, , null safety. , sound null safety : null safety , .





null-safe- Dart, Flutter, Firebase Material. : pub.dev null safety. 





, : 98% 100 , 78% 250, 57% 500 null safety . , pub.dev null safety. , pub.dev .





sound null safety

, Dart (sound). Dart 100% , non-nullable null. Dart non-nullable, non-nullable. Dart, sound null safety Swift, .





Sound null safety Dart : . Dart , non-nullable null, . , (AOT) Dart , nulls: , null.





Dart FFI Dart C

Dart FFI C , C , . Dart 2.12, Dart FFI - . , (struct) .





C , . FFI , Dart 2.12 . C, , :





struct Link {

 double value;

 Link* next;

};

void MoveByReference(Link* link) {

 link->value = link->value + 10.0;

}

Coord MoveByValue(Link link) {

 link.value = link.value + 10.0;

 return link;

}
      
      



C API . . :





struct Wheel {

 int spokes;

};

struct Bike {

 struct Wheel front;

 struct Wheel rear;

 int buildYear;

};
      
      



Dart 2.12 FFI .





API

FFI, , API.





( #44622), . , Opaque



. dart:ffi



โ€“ sizeOf



, elementAt



ref



, ( #44621). package:ff



i , . :





// Allocate a pointer to an Utf8 array, fill it from a Dart string,

// pass it to a C function, convert the result, and free the arg.

//

// Before API change:

final pointer = allocate<Int8>(count: 10);

free(pointer);

final arg = Utf8.toUtf8('Michael');

var result = helloWorldInC(arg);

print(Utf8.fromUtf8(result);

free(arg);

// After API change:

final pointer = calloc<Int8>(10);

calloc.free(pointer);

final arg = 'Michael'.toNativeUtf8();

var result = helloWorldInC(arg);

print(result.toDartString);

calloc.free(arg);
      
      



FFI

API , Dart , C. , , FFI C. , : package:ffigen.





FFI

, FFI , FFI , . :





  • , ABI , int, long, size_t (#36140)





  • Inline (#35763)





  • (#38158)





  • - (#38491)





  • Dart (#35770; C)





FFI

Dart FFI API C. :





  • open_file API, . FFI API (Windows, macOS Linux).





  • win32 API Win32, API Windows Dart.





  • objectbox โ€“ ยซยป , C.





  • tflite_flutter FFI Dart TensorFlow Lite API.





Dart ?

Sound null safety โ€“ , Dart . , . , .





(type aliases) (#65): , . , typedef :





typedef IntList = List<int>;

IntList il = [1,2,3];
      
      



(triple-shift operator) (#120): , >>>, .





- (Generic metadata annotations) (#1297): - , .





(Static meta-programming) (#1482): โ€” Dart, Dart , Rust function builder- Swift. . , , .





Dart 2.12

Dart 2.12 sound null safety FFI SDK Dart 2.12 Flutter 2.0. null safety Dart Flutter. , , Dart issue tracker.





, pub.dev, , sound null safety. null safety, , .





, !





sound null safety FFI. , : @dart_lang.








All Articles