Skip to content

Instantly share code, notes, and snippets.

@Sfshaza
Sfshaza / main.dart
Last active April 26, 2017 23:56
Java-to-Dart codelab: Starting Rectangle example
import 'dart:math';
class Rectangle {
int width;
int height;
Point origin;
}
main() {} // Included main() to suppress uncaught exception.
@Sfshaza
Sfshaza / main.dart
Last active May 3, 2018 21:58
Java-to-Dart codelab: Final Bicycle example
class Bicycle {
int cadence;
int _speed;
int gear;
Bicycle(this.cadence, this._speed, this.gear);
int get speed => _speed;
void applyBrake(int decrement) {
@Sfshaza
Sfshaza / main.dart
Last active December 28, 2022 12:11
Java-to-Dart codelab: Basic bicycle example
class Bicycle {
int cadence;
int speed;
int gear;
Bicycle(this.cadence, this.speed, this.gear);
@override
String toString() => 'Bicycle: $speed mph';
}
@Sfshaza
Sfshaza / main.dart
Last active June 2, 2023 04:49
Java-to-Dart codelab: Starting point
void main() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
}
@Sfshaza
Sfshaza / main.dart
Last active December 13, 2016 18:11
Sound Dart Guide: generic type assignment
// Enable strong mode by clicking Strong mode checkbox in lower right.
// The code passes strong mode static analysis.
// Follow the instructions in the generic type assignment below.
class Animal {
void feed() {}
}
class Alligator extends Animal {}
@Sfshaza
Sfshaza / main.dart
Created December 13, 2016 16:55
Sound Dart Guide: unsound example
// The code passes static non-strong mode static analysis.
// Enable strong mode by clicking Strong mode checkbox in lower right.
// The code now raises a warning.
void fn(List<int> a) {
print(a);
}
main() {
var list = []; // Add <int> before the [] to pass strong mode
@Sfshaza
Sfshaza / main.dart
Last active December 13, 2016 16:48
Sound Dart Guide: unsound example
// The code passes static non-strong mode static analysis.
// Enable strong mode by clicking Strong mode checkbox in lower right.
// The code now raises a warning.
void fn(List<int> a) {
print(a);
}
main() {
var list = []; // Add <int> before the [] to pass strong mode
@Sfshaza
Sfshaza / main.dart
Last active September 12, 2016 23:43
anonymous functions
main() {
var list = ['apples', 'oranges', 'grapes', 'bananas', 'plums'];
list.forEach((i) {
print(list.indexOf(i).toString() + ': ' + i);
});
}
@Sfshaza
Sfshaza / main.dart
Last active October 5, 2015 17:23
optional_types_article
// This example, part of the
// https://www.dartlang.org/articles/optional-types
// article, demonstrates a static warning in Dart.
class Point {
final num x, y;
Point(this.x, this.y);
Point operator +(Point other) {
return new Point(x + other.x, y + other.y);
}
@Sfshaza
Sfshaza / main.dart
Last active September 9, 2015 17:06
futures/waiting-on-futures
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
void main() {
Future.wait([expensiveA(), expensiveB(), expensiveC()])
.then((List responses) => chooseBestResponse(responses))
.catchError((e) => handleError(e));