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

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. Why Dart? • A consistent language • Great standard library

    • Batteries included ◦ pub ◦ dartfmt ◦ dartanalyzer • General purpose language
  2. A consistent language • Everything is an object • Optional

    static types • Async/Await • Library system
  3. Batteries included • dart: The interpreter • pub: Package manager

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

    rpc / redstone • Mobile: fluffer • Raspberry-Pi, ...
  5. Optional types String name = “Sebastian”; var firstName = “Sebastian”;

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

    } test(‘s1’, ‘s2’); String test2(String arg1, {String arg2}) { // ... } test2(‘s1’, arg2: ‘s2’);
  7. Implicit Getters/Setters User sebastian = new User(‘Sebastian’, 29); print(sebastian.firstName); //

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

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

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

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

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

    } or doSomething() { longTask().then(() { // … }); }
  13. Installing Dart > brew install dart or > curl -O

    dart.zip https://prettylongurl > unzip dart.zip
  14. 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; }
  15. 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; }
  16. 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; }
  17. 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; }
  18. Generate JSON Map _handleRequest(token) { _validateToken(token); Map response = {

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

    'response_type': 'in_channel', 'text': _getQuote()}; return response; }
  20. 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(); }
  21. 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(); }
  22. 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(); }
  23. 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); } }
  24. 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); } }
  25. 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); } }
  26. 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); } }
  27. What else? • AngularDart / PolymerDart • Flutter.io (Native Apps

    written in Dart) • Dartino (Dart on embedded devices)