Skip to main content

New site for Dart news and articles

For the latest Dart news, visit our new blog at  https://medium.com/dartlang .

Dart I/O library now uses Future for async

Posted by Mads Ager


We heard your feedback for an easier-to-use I/O library, so we just landed a change to make dart:io use futures for single-shot async methods.

For example, where you used to write:

var f = new File('myfile');
f.onError = (e) => print(e);
f.exists((b) => print('exists: $b'));


you now write:

var f = new File('myfile');
var existsFuture = f.exists();
existsFuture.then((b) => print('exists: $b'));
existsFuture.handleException((e) {
 print(e);
 return true;
});


This change has multiple advantages:
  • It is more consistent with the rest of the Dart APIs to use Futures.
  • File and Directory objects are now immutable.
  • Error handling can be done locally per operation instead of having a global error handler and you get the benefit of the error handling mechanisms built into futures.
The details are in the code reviewWe have also updated the Dart I/O API docs.


As always, please share your feedback with us in the Dart mailing list. Thanks for trying Dart!