dart 

Send to Kindle
home » snippets » dart


Pages
annotations        
api        
ast        
async        
bugs        
build        
dart2js        
dartium        
datetime        
doc_comments        
futures        
html        
http        
inheritance        
int        
invoking        
js        
json        
libraries        
list        
logging        
md5        
observatory        
observe        
operators        
polymer        
pub        
string        
testing        
timer        



Snippets

Detecting if you're running Dartium

# Javascript code.
if (navigator.userAgent.indexOf('(Dart)') != -1) {
  // We have dart.
}

ps:  The older method of checking for navigator.webkitStartDart being defined no longer works.

Detecting if you're running in dart2js

// identical(1, 1.0) is true for dart2js and false for the VM.
bool runningDart2Js = identical(1, 1.0);

Detecting checked mode

void ensureCheckedMode() {
  try {
    Object a = "abc";
    int b = a;
    print(b);  // ensures that the code is not tree-shaken off
    throw new StateError("Checked mode is disabled. Use option -c.");
  } on TypeError {
    // expected
  }
}

Reading entire stream as a string

import 'dart:async';
import 'dart:io';


String readText({Stream stream}) {
  var parts = <String>[];
  var completer = new Completer();

  stream
      .transform(new StringDecoder())
      .listen(
          (String text) => parts.add(text),
          onDone: () => completer.complete(parts.join()),
          onError: (e) => completer.completeError(e));
  return completer.future;
}

main() {
    readText(stream: stdin)
        .then((text) => print('<stdin> was "$text"'));
}