This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Populate DB | |
var books = [ | |
new Book() | |
..title = "Head First Design Patterns" | |
..authors = (new ManagedSet() | |
..add(new Author()..name = "Bert Bates") | |
..add(new Author()..name = "Kathy Sierra") | |
..add(new Author()..name = "Eric Freeman")) | |
..year = 2004, | |
new Book() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query for books | |
await Future.forEach(books, (Book b) async { | |
var query = new Query<Book>()..values = b; | |
var insertedBook = await query.insert(); | |
// Query for authors | |
await Future.forEach(b.authors, (Author a) async { | |
var query = new Query<Author>() | |
..values = a | |
..values.book = insertedBook; // Reference to its associated book, which becomes a foreign key column `book_id` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
// ... | |
test("GET /books returns list of books", () async { | |
// .. | |
// .. | |
expectResponse(response, 200, body: allOf([ | |
hasLength(greaterThan(0)), | |
everyElement(partial( | |
{ | |
"title": isString, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .. | |
class BooksController extends HTTPController { | |
@httpGet | |
Future<Response> getAllBooks() async { | |
var query = new Query<Book>()..join(set: (book) => book.authors); | |
return new Response.ok(await query.fetch()); | |
} | |
// .. | |
// .. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .. | |
class BooksController extends HTTPController { | |
//.. | |
@httpPost | |
Future<Response> addBook(@HTTPBody() Book book) async { | |
var query = new Query<Book>()..values = book; | |
var insertedBook = await query.insert(); | |
// Insert authors from payload | |
await Future.forEach(book.authors, (Author a) async { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
void main() { | |
List<String> alphabets = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
var stream = new Stream.fromIterable(alphabets); | |
var count = 1; | |
stream.listen((char) { | |
Timer(Duration(seconds: 1 * count), () { | |
print(char); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// Variables and constants | |
var edition = 2; | |
int year = 2011; | |
const language = 'Dart'; | |
String name; // == null | |
// Number | |
var num1 = 5; | |
var num2 = 10.0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// forEach() | |
var fruits = ['banana', 'pineapple', 'watermelon']; | |
fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon | |
print('\n---\n'); | |
// map() | |
var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<span class="clock"></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
var user = { | |
"firstName": "Tom", | |
"age": 25, | |
}; | |
// 1. addAll() | |
user.addAll({ | |
"lastName": "Smith", | |
"age": 26, |