Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dart Flight School SF: Intro to Dart

Dan Schultz
February 22, 2014

Dart Flight School SF: Intro to Dart

An introduction to the Dart language and SDK. Presented at the Dart Flight School event in SF.

Dan Schultz

February 22, 2014
Tweet

More Decks by Dan Schultz

Other Decks in Technology

Transcript

  1. ACCESSING NON-EXISTENT PROPERTIES It’ll run but doesn’t do the right

    thing var request = new XMLHttpRequest(); ! request.onreadystatechange = function() { if (request.readystate == 4) { console.log('Request done: ' + request.responseText); } };
  2. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR FA M IL IA R
  3. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR NAMESPACES! FA M IL IA R
  4. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR CODE IMPORTS! FA M IL IA R
  5. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR FAMILIAR FA M IL IA R
  6. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR TERSE FA M IL IA R
  7. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR OPERATOR OVERLOADING FA M IL IA R
  8. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR ONE LINE FUNCTIONS FA M IL IA R
  9. library myApp.geom; ! import "package:hash/hash.dart"; ! class Point { num

    x; num y; ! Point(this.x, this.y); ! Point operator +(Point other) { return new Point(x + other.x, y + other.y); } String toString() => "{x: $x, y: $y}"; } LANGUAGE TOUR STRING INTERPOLATION FA M IL IA R
  10. LEXICAL SCOPING FA M IL IA R class CustomButton {

    Element _hitArea; ! CustomButton() { _hitArea.onClick.listen((event) { this.sayHi(); }); } ! void sayHi() { print("Hi!"); } }
  11. MIXINS N E W IS H OBSERVABLE Don’t pollute your

    inheritance trees PERSON USER Wait, dude.. am I really a Persistable? PERSISTABLE
  12. MIXINS N E W IS H PERSON Don’t pollute your

    inheritance trees PERSISTABLE USER MIXIN OBSERVABLE
  13. MIXINS N E W IS H Don’t pollute your inheritance

    trees class Persistable { save() { … } load() { … } toJson() { … } } class } class Observable { Stream get changes { … } ! void changed(property, value) { … } } void var user }
  14. MIXINS N E W IS H Don’t pollute your inheritance

    trees class User extends Person with Persistable, Observable { } void var user } APPLY THE MIXIN class save() { … } load() toJson() } class Stream get changes ! }
  15. MIXINS N E W IS H Don’t pollute your inheritance

    trees class User extends Person with Persistable, Observable { } void main() { var user = new User(); user.save(); } APPLY THE MIXIN USE THE MIXIN’S METHODS class save() { … } load() toJson() } class Stream get changes ! }
  16. ANNOTATIONS N E W IS H Create your own! class

    User extends Person with Persistable, Observable { @bindable String name; } FIND TAGS USING REFLECTION
  17. CORE LIBRARIES Better interoperability between packages PERSISTENCE.dart import 'dart:async'; !

    class Persistable { Future save() { … } } S3.dart import ‘dart:async'; ! class S3Object { Future store() { … } }
  18. ASYNC Futures: I promise you a value api.getCurrentUser() .then((user) =>

    api.loadUserProjects(user.id)) .then((projects) => api.loadProject(projects.first.id)) .then((project) => showProject(project)) .catchError((error) => printError(error));
  19. ASYNC Streams: A series of async events • Clicks from

    a button • DOM events • Rows loaded from a database • Bytes from a file
  20. ASYNC Streams: A series of async events document.onKeyPress .takeWhile((event) =>

    event.keyCode != 13) .map((event) => new String.fromCharCode(event.keyCode)) .toList() // Returns a Future of characters .then((keys) => print(keys.join(",")));
  21. HTML XMLHttpRequest = HttpRequest HttpRequest.request("/users", method: "POST", sendData: user.toJson(), requestHeaders:

    {'Content-Type': 'application/json'}) .then((request) { users.add(JSON.decode(request.responseText)); }) .catchError((error) => print(error));
  22. Q&A