$30 off During Our Annual Pro Sale. View Details »

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!

Sebastian

June 14, 2016
Tweet

More Decks by Sebastian

Other Decks in Programming

Transcript

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

    View Slide

  2. View Slide

  3. View Slide

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

    View Slide

  5. Why Dart?
    ● A consistent language
    ● Great standard library
    ● Batteries included
    ○ pub
    ○ dartfmt
    ○ dartanalyzer
    ● General purpose language

    View Slide

  6. Popular

    View Slide

  7. TIOBE Index (June 2016)

    View Slide

  8. A consistent language
    ● Everything is an object
    ● Optional static types
    ● Async/Await
    ● Library system

    View Slide

  9. Great standard library
    ● dart:core
    ● dart:async
    ● dart:math
    ● dart:html or dart:io
    ● dart:convert

    View Slide

  10. Batteries included
    ● dart: The interpreter
    ● pub: Package manager
    ● dartfmt: Opinionated formatter
    ● dartanalyzer: Static code analyzer
    ● dart2js: Transpiler
    ● ...

    View Slide

  11. General purpose language
    ● Client-Side: AngularDart / PolymerDart
    ● Server-Side: rpc / redstone
    ● Mobile: fluffer
    ● Raspberry-Pi, ...

    View Slide

  12. Language Features
    https://www.dropbox.com/s/tfrndnh2i883wbs/289H.jpg?dl=1

    View Slide

  13. Optional types
    String name = “Sebastian”;
    var firstName = “Sebastian”;
    assert(name == firstName);
    String fullName(String firstName,var lastName) {
    return firstName + “ “ + lastName;
    }

    View Slide

  14. Function arguments
    String test(String arg1, [String arg2]) {
    // ...
    }
    test(‘s1’, ‘s2’);
    String test2(String arg1, {String arg2}) {
    // ...
    }
    test2(‘s1’, arg2: ‘s2’);

    View Slide

  15. Constructors
    class User {
    String firstName;
    int age;
    User(this.firstName, this.age);
    }

    View Slide

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

    View Slide

  17. Implicit Getters/Setters
    class User {
    String _firstName;
    int _age;
    String get firstName {
    Return _firstName;
    }
    void set age(age) {
    _age = age < 21 ? 0 : age;
    }
    }

    View Slide

  18. Implicit Getters/Setters
    User sebastian = new User(‘Sebastian’, 29);
    print(sebastian.firstName); // “Sebastian”
    sebastian.age = 31;
    print(sebastian.age); // Error

    View Slide

  19. Exceptions
    void letMeFail() {
    throw ‘This is my error message’;
    }
    try {
    letMeFail();
    } on String catch s {
    print(s);
    } catch() {
    print(‘Generic Exception’);
    }

    View Slide

  20. String interpolation
    var firstName = ‘Sebastian’;
    var lastName = ‘Göttschkes’;
    String fullName(firstName, lastName) {
    return ‘${firstName} ${lastName}’;
    }
    print(‘${fullName(firstName, lastName)}’);

    View Slide

  21. Async / Await
    doSomething() async {
    await longTask();
    // ...
    }
    or
    doSomething() {
    longTask().then(() {
    // …
    });
    }

    View Slide

  22. Building a (Slack) Bot
    https://static.pexels.com/photos/6069/grass-lawn-green-wooden-6069.jpg

    View Slide

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

    View Slide

  24. Installing Dart
    > brew install dart
    or
    > curl -O dart.zip https://prettylongurl
    > unzip dart.zip

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  28. Data
    > cat data/quotes.txt
    Stay hungry. Stay foolish.:Steve Jobs

    View Slide

  29. Check token
    void _validateToken(String token) {
    if (token != expectedToken) {
    throw 'Tokens do not match!';
    }
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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();
    }

    View Slide

  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();
    }

    View Slide

  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();
    }

    View Slide

  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);
    }
    }

    View Slide

  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);
    }
    }

    View Slide

  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);
    }
    }

    View Slide

  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);
    }
    }

    View Slide

  43. Deployment

    View Slide

  44. View Slide

  45. View Slide

  46. But what about ...

    View Slide

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

    View Slide

  48. View Slide

  49. View Slide

  50. What else?

    View Slide

  51. What else?
    ● AngularDart / PolymerDart
    ● Flutter.io (Native Apps written in Dart)
    ● Dartino (Dart on embedded devices)

    View Slide

  52. https://www.dropbox.com/s/6z5aiwa8l09g2pa/86H.jpg

    View Slide