Dart
Published in

Dart

Dart 2.18: Objective-C & Swift interop

Enhanced interoperability, platform-specific networking, improved type inference, and an important update on our null safety language roadmap

Introducing Dart to Objective-C & Swift interop

We previewed the Dart foreign function interface (FFI) for calling native C APIs in 2020 and released it in Dart 2.12 in March 2021. Since that release, a large selection of packages have taken advantage of this feature to integrate with existing native C APIs. A few examples include file_picker, printing, win32, objectbox, realm, isar, tflite_flutter, and dbus.

Time Zone example using Objective-C

macOS has an API for querying time zones information exposed on the NSTimeZone class. You can query this API for the time zone and UTC time zone offset that the user has configured for their device.

Timezone name: Europe/Copenhagen
Timezone offset GMT: 2 hours

Time Zone example using Using Dart

Let’s replicate this result with Dart using the new Objective-C interop.

$ dart create timezones
$ dart run ffigen

Platform-specific http libraries

Dart includes a general, multi-platform http library. This library allows you to write code without concern for platform specifics. On occasion, you might want to write code specific to a particular host platform’s networking APIs.

Cupertino http library example

The following example sets a Flutter app’s http client to use the cupertino_http library on macOS and iOS and Dart’s regular http library from dart:io on other platforms:

Platform-specific networking in multi-platform apps

As we designed this feature, the goal remained to keep apps as multi-platform as possible. To meet this goal, we kept our general multi-platform http API set for basic http operations, and allowed for configuring per-platform which networking library to use. You can minimize the amount of platform-specific code that you need to write by using the package:http Client API. This API can be configured per-platform but used in a platform-independent manner.

Improved type inference

Dart uses many generic functions. Consider the fold method, which reduces a collection of elements to a single value. The following example calculates the sum of a list of integers:

List<int> numbers = [1, 2, 3];
final sum = numbers.fold(0, (x, y) => x + y);
print(‘The sum of $numbers is $sum’);
line 2 • The operator ‘+’ can’t be unconditionally invoked because the receiver can be ‘null’.
final sum = numbers.fold(0, (int x, int y) => x + y);

Async performance improvements

This version of Dart improves how the Dart VM applies the async method and the async*/sync* generator functions. This reduces code size. On two large internal Google apps, we saw a reduction of the AOT snapshot size of around 10%. We also saw a performance increase across our microbenchmarks.

pub.dev improvements

In conjunction with the 2.18 release, we’ve made two changes on the pub.dev package repository.

A few breaking changes

Dart has a strong focus on simplicity and learnability. We’re constantly trying to keep a careful balance when adding new capabilities. One method to keep things simple is to remove historic functionality and APIs with little use or better replacements. Dart 2.18 cleans up items in this category, including a few smaller breaking changes:

  • We added the unified dart CLI developer tool back in October 2020. In 2.18, we completed the transition. This release removes the last two deprecated tools dart2js (use dart compile js) and dartanalyzer (use dart analyze).
  • With the introduction of language versioning, pub generates a new resolution file: .dart_tool/package_config.json. The previous file, .packages, used a format that couldn’t contain versions. We discontinued using the .packages file. If you have any .packages files, you can delete them.
  • Mixins of classes that don’t extend Object can’t be used (breaking change #48167). This behavior was never intended.
  • The uri property of dart:io’s RedirectException has been changed to nullable (breaking change #49045).
  • Constants in dart:io’s networking APIs following the SCREAMING_SNAKE convention have been removed (breaking change #34218; previously deprecated). Use the corresponding lowerCamelCase constants instead.
  • The Dart VM no longer restores the initial terminal settings upon exit. Programs that change the Stdin settings lineMode and echoMode are now responsible for restoring the settings upon program exit (breaking change #45630).

Null safety update

We’re very pleased to see the wide usage of null safety since its beta release in November 2020 and the Dart 2.12 release in March 2021.

An important null safety roadmap update

Supporting both unsound and sound null safety adds overhead and complexity.

Summary

The new support for interop, networking, type inference, and pub.dev is available today. To get started, you can directly download the Dart 2.18 release, or get it embedded as part of today’s Flutter 3.3 SDK release.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michael Thomsen

Product Manager working on Dart and Flutter. Helping developers is my passion!