Skip to content

Instantly share code, notes, and snippets.

View matanlurey's full-sized avatar

Matan Lurey matanlurey

View GitHub Profile
@matanlurey
matanlurey / futures.dart
Created February 22, 2017 20:30
A simple example of using the Future API
import 'dart:async';
main() {
// Prints '2017' after a second.
computeSomething().then(print);
}
Future<int> computeSomething() {
// Pretend to do a computation, and take some time.
return new Future.delayed(const Duration(seconds: 1), () => 2017);
@matanlurey
matanlurey / unnecessary_completer_1.dart
Last active February 22, 2017 20:40
An example of an unnecessary Completer usage in Dart
import 'dart:async';
final messageStorage = <String>[];
Future save(String message) {
var completer = new Completer();
new Future(() => messagesStorage.add(message)).then((_) => completer.complete());
return completer.future;
}
@matanlurey
matanlurey / fixed_completer1.dart
Created February 22, 2017 20:44
Replaced Completer usage
import 'dart:async';
final messageStorage = <String>[];
Future save(String message) => new Future(() => messageStorage.add(message));
Future<List<String> getAll() => new Future(() => messageStorage);
@matanlurey
matanlurey / blob.json
Created March 9, 2017 23:33
A large JSON blob useful for benchmarking deserialization
{
"Cxxxxxxxxxxx": "axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"axx": "Gxx",
"mxxxxx": "Gxx",
"qxxxx": "pxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"rxxxxxxxxxx": "dxxx",
"rxxxxxx": "Mxxxxxxxxxxxxxxxxxxxxx",
"rxxxxxxx": {
"ixxxx": [
{
/// Returns a future that completes when a [Resolver] for Dart [sourceCode].
///
/// ```
/// await resolveSource(r'''
/// import 'dart:collection';
///
/// Map createMap() => {};
/// ''')
/// ```
///
@matanlurey
matanlurey / const.js
Created April 24, 2017 03:05
An example of using "const" in ES6
// Define USER as a constant and give it an initial value.
const USER = { name: 'Joe'; }
// This will throw an error.
USER = {};
// But this will not.
USER.name = 'Jill';
@matanlurey
matanlurey / const.dart
Created April 24, 2017 03:10
An example of using const in Dart
main() {
const user = const {'name': 'Joe'};
// Static error: "Constant variables cannot be assigned a value".
user = {};
// Runtime error: "Unsupported operation: Cannot modify unmodifiable Map".
user['name'] = 'Jill';
}
@matanlurey
matanlurey / const.dart
Created April 24, 2017 03:13
An example of defining a new user-type with a const constructor
class User {
final String name;
const User(this.name);
}
main() {
const user = const User('Joe');
}
@matanlurey
matanlurey / canonical_const.dart
Created April 24, 2017 03:20
Const instances in Dart are canonicalized
class User {
final String name;
final List<String> cars;
User(this.name, {this.cars});
}
main() {
for (var i = 0; i < 100; i++) {
const users = const {
@matanlurey
matanlurey / immutable.dart
Created April 24, 2017 03:26
An example of using the @immutable annotation
import 'pacakge:meta/meta.dart';
// Error: This class inherits from a class marked as @immutable, and therefore
// should be immutable (all instance fields must be final).
@immutable
class User {
String name;
}