Skip to main content

New Streams API with Dart Milestone 3

We are proud to announce the Milestone 3 release of the Dart SDK, which provides a new, cohesive API for asynchronous programming and a unified model for events. Some of the new or improved classes in this release include Iterable, Stream, and Future.

Dart's M3 release delivers on a common developer request for a more unified approach to events. New and updated in M3, the Iterable class and the Stream class now provide synchronous and asynchronous models, respectively, for a common theme: a sequence of items.

The Iterable class is now much more prominent in Dart and is the ancestor of common collections like List and Set. Many APIs now return Iterable instead of a specific collection. You can map, join, skip, take, and more on an Iterable. Here is an example:


var names = getAccounts().where((a) => a.isPlatinum)

.map((a) => a.fullName)
.toList();

The Stream class (and its dart:async library) is new, and it provides an asynchronous stream of events. An event can be any Dart object, which makes Streams very flexible. Consumers of a Stream can listen for events, and streams can be piped, transformed, filtered, and more. Streams are now the unified way to deliver asynchronous events, and we are working to apply them across HTML, I/O, isolates, and more. Here is an example of using streams with the HTML library, treating clicks as a stream of events:

query('#button').onClick.listen((e) => submitForm());

The Future class, which represents a single value available in the future, was cleaned up and is now much easier to use. In many cases this allows you to write asynchronous code very similar to its synchronous counterpart:



SynchronousAsynchronous
bool writeFile(String data, File file) {
try {
  var io = file.openSync(FileMode.WRITE);
  io.writeStringSync(data);
  io.closeSync();
  return true;
} catch (error) {
  return false;
 }

}
Future<bool> writeFile(String data, File file) {
return file.open(FileMode.WRITE)
  .then((io) => io.writeString(data))
  .then((io) => io.close())
  .then((io) => true)
  .catchError((error) => false);
}

Some of these changes are not backwards compatible. Luckily, you can use Dart Editor's Clean Up feature to help automatically port your code from the old API to the new API. Here is an example of Clean Up at work:






Over the following months we will continue to work on the libraries, applying the new asynchronous models introduced in M3. We always appreciate the feedback, please let us know what you think at dartbug.com and misc@dartlang.org.

To get started, you can download the Dart SDK from dartlang.org, learn about the language and libraries, and follow our tutorials. If you already have Dart Editor, it can auto-update to the latest release for you.


Posted by Florian Loitsch, Software Engineer and Corelib Hacker

Popular posts from this blog

Dart in 2016: The fastest growing programming language at Google, 2nd fastest growing in TIOBE Index

Dart was the fastest growing programming language at Google in 2016 with millions of lines of code written. It also made it to TIOBE Index Top 20 this month (see TIOBE's methodology).

It takes time to build something as ambitious as Dart and, in some ways, Dart is still in its infancy. But we're glad the hard work is starting to pay off.

Many thanks to our amazing community!

We're going to celebrate by ... releasing 1.22 next week (as per our usual 6 week release schedule).

AngularDart is going all Dart

Until now, the multiple language flavors of Angular 2 were written as TypeScript source, and then automatically compiled to both JavaScript and Dart. We're happy to announce that we’re splitting the Angular 2 codebase into two flavors – a Dart version and a TypeScript/JavaScript version – and creating a dedicated AngularDart team.

This is amazing news for Dart developers because:

The framework will feel more like idiomatic Dart.It will make use of Dart features that couldn't work with the TypeScript flavor. It will be faster.
This is equally great news for our TypeScript and JavaScript developers, by the way. Cleaner API, performance gains, easier contributions. Read more on the Angular blog.

Angular 2 for Dart is used by many teams at Google. Most famously by the AdWords team, but many other Google teams build large, mobile-friendly web apps. Some of the top requests from these teams were: make the API feel like Dart, provide a faster edit-refresh cycle, and improve applicat…

The new AdWords UI uses Dart — we asked why

Google just announced a re-designed AdWords experience. In case you’re not familiar with AdWords: businesses use it to advertise on google.com and partner websites. Advertising makes up majority of Google’s revenue, so when Google decides to completely redo the customer-facing front end to it, it’s a big deal. The Dart team is proud to say that this new front end is built with Dart and Angular 2. Whenever you asked us whether Google is ‘even using Dart for anything,’ this is what we had in mind but couldn’t say aloud. Until now. We asked Joshy Joseph, the primary technical lead on the project, some questions. Joshy is focusing on things like infrastructure, application latency and development velocity, so he’s the right person to ask about Dart.Q: What exactly did we launch on Monday?It’s a complete redesign of the AdWords customer experience that is rolling out slowly as a test to a small initial set of advertisers. The most noticeable thing is probably the Material Design look and f…