SlideShare a Scribd company logo
Dart Workshop
by Dmitry Buzdin
July 2013, Riga
You should be here today!
Brought toYou By
Workshop
Preparation
Get Dart
http://www.dartlang.org/#get-started
WhatYou Get
• Dart SDK
• Dart Editor (Eclipse-based)
• DartiumVM
• Chromium with DartVM
• Command line tools
• Samples
IDE
Prerequisites
• dart installed
• dart2js installed
• pub installed
• Dartium installed
• IDE (Dart Editor, IntelliJ, Sublime,VIM)
Knowledge Required
• HTML/CSS basics
• JavaScript basics
• OOP basics
• FP basics
Join the
Dart
Side
Why Dart?
• JavaScript is broken
• Quest for holy grail is on
• One language for client and server
• Node.js is not an option
DartVM
• Speed
• Multithreading via Isolates
Dart Compiler
• Compiles to JavaScript
• Runs in all modern browsers
• DartVM is faster thanV8
• (according to Google)
Dart Language
• Similar to Java and JavaScript
http://try.dartlang.org/
Main Features
• Functions
• Closures
• Concurrency
• Modularity
• OOP
http://www.dartlang.org/docs/dart-up-and-running/
contents/ch02.html
Optional Type Safety
• Checked during development
• Compiled-out in production
Dart Timeline
• Inspired by Java, JavaScript, GWT
• Revealed in October 2011
• Frequent public releases
• June 19, 2013 First Beta version
• Production ~2014
Warning: Dart is hot!
Code shown will not probably
work after two months
Workshop
Dart Workshop
Browser
DartVM
Client Dart
Code
DartVM
Server Dart
Code
MongoDB
HTTP
HTTP
Ways of Working
• Working in pairs
• Writing automated tests
• 5 Steps to complete
• Solutions available online
https://github.com/buzdin/dart-workshop
• Part I : Hello, Dart!
• Part II : Integrating Google Maps
• Part III: Dynamic HTML
• Part IV:Adding Server-Side
• PartV:Adding MongoDB
Part I : Hello Dart
Tasks
• Learn application directory structure
• Run client and server side code
• Check output in Dartium console
• Write new unit tests
• Check that they work
• Learn to use debugger
Base Project Structure
Modules
Tests
Client
Dependencies
Symbolic
Link
http://pub.dartlang.org/doc/package-layout.html
Pub Package Manager
• Same as npm (JavaScript) or Maven (Java)
• pubspec.yaml - application dependencies
• pubspec.lock - exact versions
http://pub.dartlang.org/
pubspec.yaml
name: dart_workshop
dependencies:
browser: any
unittest: any
Latest version
http://pub.dartlang.org/doc/pubspec.html
Fetch Dependencies
pub install
Resolving dependencies................
Downloading fukiya 0.1.9 from hosted...
Downloading formler 0.0.8 from hosted...
Dependencies installed!
Unit Testing
void main() {
test('QuickSort', () =>
expect(quickSort([5, 4, 3, 2, 1]),
orderedEquals([1, 2, 3, 4, 5]))
);
test('Partition', () {
List array = [3, 2, 1];
int index = _partition(array, 0, array.length-1, 1);
expect(index, equals(1));
expect(array, orderedEquals([1, 2, 3]));
});
}
Bootstrap Dart (1)
<!DOCTYPE html>
<html>
<head>
<title>Dart Workshop</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<script type="application/dart" src="main.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
Bootstrap Dart (1I)
// This is Dart Application
void main() {
sayHello("Hello, Dart!");
}
void sayHello() {
print("Hello");
}
Running Dart
Debugging
You Learned
• Dart project structure
• Running and debugging Dart
• Writing and running unit tests
Part II : Integrating
Google Maps
Tasks
• Integrate Google Maps
• Add event handlers
• Draw custom map markers
import 'dart:html';
import 'package:js/js.dart' as js;
import 'package:google_maps/google_maps.dart';
void main() {
js.context.google.maps.visualRefresh = true;
final mapOptions = new MapOptions()
..zoom = 13
..center = new LatLng(56.946843515558456, 24.13162512207032)
..mapTypeId = MapTypeId.ROADMAP
;
final map = new GMap(query("#map_canvas"), mapOptions);
js.retain(map);
}
Adding Google Maps
<div id="map_canvas"></div>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
dependencies:
google_maps: any
#map_canvas {
height: 100%;
margin: 0;
}
Handling Map Events
map.onCenterChanged.listen((ignore) {
print(map.center);
});
map.onZoomChanged.listen((ignore) {
print(map.zoom);
});
map.onClick.listen((event) {
print(event.latLng);
});
Drawing Markers
var marker = new Marker(
new MarkerOptions()
..position = new LatLng(56.946, 24.131)
..map = map
..title = 'Best Place in the World!'
..icon = 'icon.png'
);
http://www.iconarchive.com/
Pick your own icon!
Event Streams
http://www.dartlang.org/articles/feet-wet-streams/
TODO
You Learned
• Adding JavaScript libraries
• Integrating Google Maps API
• Using event Streams
• Picking icons
Part III : Dynamic
HTML
Tasks
• Draw input form for place name and icon
• Show/hide DOM elements
• Attach custom DOM event handlers
JQuery in Dart
import 'dart:html';
Element elem1 = query('#an-id'); // Find an element by id (an-id).
Element elem2 = query('.a-class'); // Find an element by class (a-class).
List<Element> elems1 = queryAll('div'); // Find all elements by tag (<div>).
List<Element> elems2 = queryAll('input[type="text"]'); // Find all text inputs.
// Find all elements with the CSS class 'class' inside of a <p>
// that is inside an element with the ID 'id'.
List<Element> elems3 = queryAll('#id p.class');
http://api.dartlang.org/docs/releases/latest/dart_html.html
Manipulating DOM
<div id="form">
<input id="name" type="text"/>
<input id="icon" type="text"/>
</div>
var name = query('#name').value;
query('#name').value = ‘New Value’;
query('#form').hidden = true;
Reacting on Events
map.onClick.listen((event) {
var name = query('#name').value;
var icon = query('#icon').value;
savePlace(name, icon, event.latLng);
});
You Learned
• JQuery is built-in in Dart
• Learned to manipulate DOM
• Learned to attach event handlers
Part IV :Adding Server
Tasks
• Draw input form for place name and icon
• Show/hide DOM elements
• Attach custom DOM event handlers
REST API
POST /api/places
GET /api/places?near=lat,long
Place JSON
{
name:“Name”,
loc: [53.2114, 24.5623],
icon:“http://iconarchive.com/icon.png”
}
HTTP Server
import 'dart:io';
import 'dart:json';
main() {
HttpServer.bind('127.0.0.1', 8080).then((server) {
server.listen((HttpRequest request) {
request.response.write(“Hello!”);
request.response.close();
});
});
}
Too much code, lets
take existing solution
Fukiya Server
http://pub.dartlang.org/packages/fukiya
void main() {
new Fukiya()
..get('/', getHandler)
..put('/', putHandler)
..delete('/', deleteHandler)
..post('/', postHandler)
..get('/testing', (FukiyaContext context) {
context.send("This is testing.");
})
..get('/:userid', getDynamicHandler)
..staticFiles('./test/static')
..use(new FukiyaFormParser())
..use(new FukiyaJsonParser())
..listen('127.0.0.1', 3333);
}
Working with JSON
String str = stringify(
{
'key': 42,
'value': 'text',
'p': ['A', 'B']
}
);
Map doc = parse(str);
import 'dart:json';
Our Web Server
import 'package:fukiya/fukiya.dart';
main() {
new Fukiya()
..get('/api/places', getHandler)
..post('/api/places', postHandler)
..staticFiles('web')
..use(new FukiyaJsonParser())
..listen('127.0.0.1', 8080);
}
> dart bin/server.dart
void postHandler(FukiyaContext context) {
print(context.params);
print(context.parsedBody);
context.send("OK");
context.jsonResponse(json);
}
Server HTTP Client
import 'package:http/http.dart' as http;
void main() {
http.get('http://127.0.0.1:8080').then((response) {
print(response.body);
});
}
http://pub.dartlang.org/packages/http
Test Example
test("should-get", () {
http.get('http://127.0.0.1:8080/api/places?near=1,2')
.then(expectAsync1((response) {
expect(response.statusCode, equals(200));
expect(response.body, equals('[]'));
}));
});
Browser Ajax Client
import 'dart:html';
import 'dart:async';
import 'dart:json';
main() {
HttpRequest.getString(uri).then(processString);
}
processString(String jsonText) {
Map object = parse(jsonText);
println(object);
}
http://www.dartlang.org/articles/json-web-service/
Posting Data
var request = new HttpRequest();
request.open('POST', '/api/places');
request.setRequestHeader("Content-Type", "application/json");
request.send(json);
Timers
new Timer.periodic(
new Duration(seconds:5), (timer) {
print("timer triggered");
loadPlaces(map);
});
Refresh list of places every few seconds
import 'dart:async';
http://api.dartlang.org/docs/releases/latest/dart_async/
Timer.html
You Learned
• Server side is “easy”
• Testing HTTP services
• Creating Ajax client
• Using JSON
PartV :Adding
MongoDB
Tasks
• Connect to real Cloud database!
• Replace in-memory database with real one
• Write tests
MongoDB
• No schema
• JSON as data format
• JSON as query language
MongoDB in 3 minutes
Database
Collection
Document
Database
Table
Record
Database Records
{
name:“Name”,
loc: [53.2114, 24.5623],
icon:“http://iconarchive.com/icon.png”
}
Database Query
db.products.find({qty: {$gt: 25}})
Mongo Dart Driver
import 'package:mongo_dart/mongo_dart.dart';
var db = new Db(url);
db.open().then((c) {
DbCollection places = db.collection("places");
places.insert({
"name": "Place", "loc": [56, 23], "icon”: "Place"
});
});
name: dart_workshop
dependencies:
mongo_dart: any
Geolocation Queries
var db = new Db(DB_URL);
db.open().then((c) {
db.ensureIndex("places", key: "loc", name: "2d");
DbCollection places = db.collection("places");
places.find(where.near("loc",
{'$geometry': {"type" : "Point", "coordinates" : [23, 23]}
}, 10000)).each((place) {
print(place);
}).then((ignore) {
db.close();
});
});
MongoLab Account
• mongodb://
dart:dartIsKing@ds035338.mongolab.com:
35338/dart-workshop
• mongo ds035338.mongolab.com:35338/
dart-workshop -u dart -p dartIsKing
You Learned
• Future based API
• Used Mongo Geolocation Queries
• Mongo Dart library sucks!
• Write full-stack Dart applications!
Conclusions
Dart is better than
JavaScript!
Dart Workshop
Dart is the future!?
Discuss!
Materials
• http://pub.dartlang.org/doc/package-layout.html
• http://yulian.kuncheff.com/blog/2013/03/21/using-intellij-slash-webstorm-to-debug-web-applications/
• http://www.youtube.com/watch?v=N8GCNilJhT4
• http://docs.mongodb.org/manual/reference/operator/query-geospatial/
• https://developers.google.com/maps/documentation/javascript/reference

More Related Content

PDF
Developing Useful APIs
PPTX
PPTX
Dart the Better JavaScript
PDF
Google Dart
PDF
Structured web programming
PDF
TDD with phpspec2
PDF
Inside the JVM - Follow the white rabbit!
PDF
Let's Play Dart
Developing Useful APIs
Dart the Better JavaScript
Google Dart
Structured web programming
TDD with phpspec2
Inside the JVM - Follow the white rabbit!
Let's Play Dart

What's hot (20)

PPTX
pebble - Building apps on pebble
PDF
JavaScript - From Birth To Closure
PPT
JavaScript Basics
PPT
A Deeper look into Javascript Basics
PDF
Code generating beans in Java
PPT
JavaScript - An Introduction
PPT
Introduction to Javascript
PDF
Effective testing with pytest
PDF
Mobile Open Day: React Native: Crossplatform fast dive
PPTX
Apache Ant
PDF
Nikita Popov "What’s new in PHP 8.0?"
PDF
Dart, Darrt, Darrrt
PDF
walkmod: An open source tool for coding conventions
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
PDF
Start dart
PDF
walkmod: quick start
PDF
walkmod - JUG talk
PDF
PHP 8: Process & Fixing Insanity
PPTX
002. Introducere in type script
pebble - Building apps on pebble
JavaScript - From Birth To Closure
JavaScript Basics
A Deeper look into Javascript Basics
Code generating beans in Java
JavaScript - An Introduction
Introduction to Javascript
Effective testing with pytest
Mobile Open Day: React Native: Crossplatform fast dive
Apache Ant
Nikita Popov "What’s new in PHP 8.0?"
Dart, Darrt, Darrrt
walkmod: An open source tool for coding conventions
TypeScript - Silver Bullet for the Full-stack Developers
Start dart
walkmod: quick start
walkmod - JUG talk
PHP 8: Process & Fixing Insanity
002. Introducere in type script
Ad

Viewers also liked (9)

PPTX
Whats New in Java 8
PDF
Riding Redis @ask.fm
PPTX
Big Data Processing Using Hadoop Infrastructure
PPTX
Архитектура Ленты на Одноклассниках
PDF
JOOQ and Flyway
PDF
Automating Your Infrastructure
PDF
Crowdsourcing Expert Performance to Improve Training at Cyber Speed
PPT
Creative Play with Technology
PPT
Java Riga Day 2011 Opening
Whats New in Java 8
Riding Redis @ask.fm
Big Data Processing Using Hadoop Infrastructure
Архитектура Ленты на Одноклассниках
JOOQ and Flyway
Automating Your Infrastructure
Crowdsourcing Expert Performance to Improve Training at Cyber Speed
Creative Play with Technology
Java Riga Day 2011 Opening
Ad

Similar to Dart Workshop (20)

PPTX
Dartprogramming
PDF
Stencil the time for vanilla web components has arrived
PDF
Stencil: The Time for Vanilla Web Components has Arrived
PDF
Idiomatic Gradle Plugin Writing
PPTX
PPTX
Jquery dojo slides
PDF
Web Components v1
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PDF
Brad Wood - CommandBox CLI
PDF
Into The Box 2018 Going live with commandbox and docker
PDF
Going live with BommandBox and docker Into The Box 2018
PDF
SF Grails - Ratpack - Compact Groovy Webapps - James Williams
PDF
SwampDragon presentation: The Copenhagen Django Meetup Group
PDF
OSCON 2014 - API Ecosystem with Scala, Scalatra, and Swagger at Netflix
PPTX
Spring Boot
PPTX
SharePoint Cincy 2012 - jQuery essentials
PDF
Dart for Java Developers
PDF
Having Fun with Play
ODP
PDF
GDG Addis - An Introduction to Django and App Engine
Dartprogramming
Stencil the time for vanilla web components has arrived
Stencil: The Time for Vanilla Web Components has Arrived
Idiomatic Gradle Plugin Writing
Jquery dojo slides
Web Components v1
Intro To JavaScript Unit Testing - Ran Mizrahi
Brad Wood - CommandBox CLI
Into The Box 2018 Going live with commandbox and docker
Going live with BommandBox and docker Into The Box 2018
SF Grails - Ratpack - Compact Groovy Webapps - James Williams
SwampDragon presentation: The Copenhagen Django Meetup Group
OSCON 2014 - API Ecosystem with Scala, Scalatra, and Swagger at Netflix
Spring Boot
SharePoint Cincy 2012 - jQuery essentials
Dart for Java Developers
Having Fun with Play
GDG Addis - An Introduction to Django and App Engine

More from Dmitry Buzdin (20)

PDF
How Payment Cards Really Work?
PDF
Как построить свой фреймворк для автотестов?
PDF
How to grow your own Microservice?
PDF
How to Build Your Own Test Automation Framework?
PDF
Delivery Pipeline for Windows Machines
PDF
Rubylight JUG Contest Results Part II
PDF
Rubylight Pattern-Matching Solutions
PDF
Refactoring to Macros with Clojure
PPTX
Poor Man's Functional Programming
PDF
Rubylight programming contest
PPTX
Continuous Delivery
PPTX
Introduction to DevOps
PDF
Thread Dump Analysis
PDF
Pragmatic Java Test Automation
PDF
Mlocjs buzdin
PDF
Web polyglot programming
PPTX
Code Structural Analysis
PDF
Google Guava
PPT
Jug Intro 20
PDF
Jug intro 18
How Payment Cards Really Work?
Как построить свой фреймворк для автотестов?
How to grow your own Microservice?
How to Build Your Own Test Automation Framework?
Delivery Pipeline for Windows Machines
Rubylight JUG Contest Results Part II
Rubylight Pattern-Matching Solutions
Refactoring to Macros with Clojure
Poor Man's Functional Programming
Rubylight programming contest
Continuous Delivery
Introduction to DevOps
Thread Dump Analysis
Pragmatic Java Test Automation
Mlocjs buzdin
Web polyglot programming
Code Structural Analysis
Google Guava
Jug Intro 20
Jug intro 18

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Big Data Technologies - Introduction.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Big Data Technologies - Introduction.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
Network Security Unit 5.pdf for BCA BBA.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology

Dart Workshop