Bullseye - An introduction to Dart

Bullseye - An introduction to Dart

Let's dive into the language straight. You do not need any knowledge of the language prior to this. We'll walk through language features with the example of a Slackbot!

Db6218c1b56d64e775eb79ea4f2889dd?s=128

Sebastian

June 14, 2016
Tweet

Transcript

  1. Bullseye: An introduction to Dart DartVienna @Sgoettschkes https://static.pexels.com/photos/21300/pexels-photo.jpg

  2. None
  3. None
  4. Why Dart? https://static.pexels.com/photos/59628/pexels-photo-59628.jpeg

  5. Why Dart? • A consistent language • Great standard library

    • Batteries included ◦ pub ◦ dartfmt ◦ dartanalyzer • General purpose language
  6. Popular

  7. TIOBE Index (June 2016)

  8. A consistent language • Everything is an object • Optional

    static types • Async/Await • Library system
  9. Great standard library • dart:core • dart:async • dart:math •

    dart:html or dart:io • dart:convert
  10. Batteries included • dart: The interpreter • pub: Package manager

    • dartfmt: Opinionated formatter • dartanalyzer: Static code analyzer • dart2js: Transpiler • ...
  11. General purpose language • Client-Side: AngularDart / PolymerDart • Server-Side:

    rpc / redstone • Mobile: fluffer • Raspberry-Pi, ...
  12. Language Features https://www.dropbox.com/s/tfrndnh2i883wbs/289H.jpg?dl=1

  13. Optional types String name = “Sebastian”; var firstName = “Sebastian”;

    assert(name == firstName); String fullName(String firstName,var lastName) { return firstName + “ “ + lastName; }
  14. Function arguments String test(String arg1, [String arg2]) { // ...

    } test(‘s1’, ‘s2’); String test2(String arg1, {String arg2}) { // ... } test2(‘s1’, arg2: ‘s2’);
  15. Constructors class User { String firstName; int age; User(this.firstName, this.age);

    }
  16. Implicit Getters/Setters User sebastian = new User(‘Sebastian’, 29); print(sebastian.firstName); //

    “Sebastian” sebastian.age = 31; print(sebastian.age); // “31”
  17. Implicit Getters/Setters class User { String _firstName; int _age; String

    get firstName { Return _firstName; } void set age(age) { _age = age < 21 ? 0 : age; } }
  18. Implicit Getters/Setters User sebastian = new User(‘Sebastian’, 29); print(sebastian.firstName); //

    “Sebastian” sebastian.age = 31; print(sebastian.age); // Error
  19. Exceptions void letMeFail() { throw ‘This is my error message’;

    } try { letMeFail(); } on String catch s { print(s); } catch() { print(‘Generic Exception’); }
  20. String interpolation var firstName = ‘Sebastian’; var lastName = ‘Göttschkes’;

    String fullName(firstName, lastName) { return ‘${firstName} ${lastName}’; } print(‘${fullName(firstName, lastName)}’);
  21. Async / Await doSomething() async { await longTask(); // ...

    } or doSomething() { longTask().then(() { // … }); }
  22. Building a (Slack) Bot https://static.pexels.com/photos/6069/grass-lawn-green-wooden-6069.jpg

  23. Architecture Response Request Check Token Get Quote Generate JSON Producto

    ❤ ❤
  24. Installing Dart > brew install dart or > curl -O

    dart.zip https://prettylongurl > unzip dart.zip
  25. Setup > cat pubspec.yaml name: producto version: 0.0.1 dependencies: redstone:

    '^0.6.0' > pub get
  26. Setup > cat pubspec.yaml name: producto version: 0.0.1 dependencies: redstone:

    '^0.6.0' > pub get
  27. Setup > cat pubspec.yaml name: producto version: 0.0.1 dependencies: redstone:

    '^0.6.0' > pub get
  28. Data > cat data/quotes.txt Stay hungry. Stay foolish.:Steve Jobs

  29. Check token void _validateToken(String token) { if (token != expectedToken)

    { throw 'Tokens do not match!'; } }
  30. Get one quote String _getQuote() { File file = new

    File('data/quotes.txt'); List<String> lines = file.readAsLinesSync(); List<String> quotes = []; lines.forEach((String line) { List<String> parts = line.split(':'); quotes.add('"${parts[0]}" - ${parts[1]}'); }); final _random = new Random(); String quote = quotes[_random.nextInt(quotes.length)]; return quote; }
  31. Get one quote String _getQuote() { File file = new

    File('data/quotes.txt'); List<String> lines = file.readAsLinesSync(); List<String> quotes = []; lines.forEach((String line) { List<String> parts = line.split(':'); quotes.add('"${parts[0]}" - ${parts[1]}'); }); final _random = new Random(); String quote = quotes[_random.nextInt(quotes.length)]; return quote; }
  32. Get one quote String _getQuote() { File file = new

    File('data/quotes.txt'); List<String> lines = file.readAsLinesSync(); List<String> quotes = []; lines.forEach((String line) { List<String> parts = line.split(':'); quotes.add('"${parts[0]}" - ${parts[1]}'); }); final _random = new Random(); String quote = quotes[_random.nextInt(quotes.length)]; return quote; }
  33. Get one quote String _getQuote() { File file = new

    File('data/quotes.txt'); List<String> lines = file.readAsLinesSync(); List<String> quotes = []; lines.forEach((String line) { List<String> parts = line.split(':'); quotes.add('"${parts[0]}" - ${parts[1]}'); }); final _random = new Random(); String quote = quotes[_random.nextInt(quotes.length)]; return quote; }
  34. Generate JSON Map _handleRequest(token) { _validateToken(token); Map response = {

    'response_type': 'in_channel', 'text': _getQuote()}; return response; }
  35. Generate JSON Map _handleRequest(token) { _validateToken(token); Map response = {

    'response_type': 'in_channel', 'text': _getQuote()}; return response; }
  36. Putting it all together import 'package:redstone/redstone.dart' as app; import 'package:producto/producto.dart';

    void main() { app.showErrorPage = false; app.setupConsoleLog(); app.start(); }
  37. Putting it all together import 'package:redstone/redstone.dart' as app; import 'package:producto/producto.dart';

    void main() { app.showErrorPage = false; app.setupConsoleLog(); app.start(); }
  38. Putting it all together import 'package:redstone/redstone.dart' as app; import 'package:producto/producto.dart';

    void main() { app.showErrorPage = false; app.setupConsoleLog(); app.start(); }
  39. Putting it all together library producto; import 'package:redstone/redstone.dart' as app;

    @app.Group('/') class Producto { static String expectedToken = 'nCssv2d6rtzGkXuJwYpEbZkr'; @app.Route('/give-me-product-love', methods: const [app.GET]) productLoveGet(@app.QueryParam('token') String token) { return _handleRequest(token); } }
  40. Putting it all together library producto; import 'package:redstone/redstone.dart' as app;

    @app.Group('/') class Producto { static String expectedToken = 'nCssv2d6rtzGkXuJwYpEbZkr'; @app.Route('/give-me-product-love', methods: const [app.GET]) productLoveGet(@app.QueryParam('token') String token) { return _handleRequest(token); } }
  41. Putting it all together library producto; import 'package:redstone/redstone.dart' as app;

    @app.Group('/') class Producto { static String expectedToken = 'nCssv2d6rtzGkXuJwYpEbZkr'; @app.Route('/give-me-product-love', methods: const [app.GET]) productLoveGet(@app.QueryParam('token') String token) { return _handleRequest(token); } }
  42. Putting it all together library producto; import 'package:redstone/redstone.dart' as app;

    @app.Group('/') class Producto { static String expectedToken = 'nCssv2d6rtzGkXuJwYpEbZkr'; @app.Route('/give-me-product-love', methods: const [app.GET]) productLoveGet(@app.QueryParam('token') String token) { return _handleRequest(token); } }
  43. Deployment

  44. None
  45. None
  46. But what about ...

  47. http://pixabay.com/static/uploads/photo/2010/12/10/08/salad-1105_640.jpg

  48. None
  49. None
  50. What else?

  51. What else? • AngularDart / PolymerDart • Flutter.io (Native Apps

    written in Dart) • Dartino (Dart on embedded devices)
  52. https://www.dropbox.com/s/6z5aiwa8l09g2pa/86H.jpg