Skip to content

Instantly share code, notes, and snippets.

@kirkdrichardson
kirkdrichardson / main.dart
Created October 12, 2019 00:47
Static methods and variables in Dart
// Static methods and variables in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/300a5a59fc1209aaa6bbbc1f1427bc4a
void main() {
print('Moon mass: ${Moon.mass}');
print('Moon mass scaled down by 10 is ${Moon.getScaledMass(10)}');
print('Moon mass scaled up by 10 is ${Moon.getScaledMass(10, false)}');
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 23:52
Interfaces in Dart
// Interfaces in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/19a8c3c203f27fd1820c004582480ff5
/*
* TLDR;
* There is no interface keyword in Dart. Instead we use a regular class with either the extends or
* implements keyword. If a subclass uses the extends keyword, the child class inherits from the
* parent class normally. Else, if the subclass uses the implements keyword, the parent class is
* treated as an interface: all inherited members must be overriden.
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 23:37
Abstract classes and methods in Dart
// Abstract classes and methods in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/847473dc3db9ba5ad9848ecf7b00ba7e
void main() {
// Expect Error! - Can't instantiate abstract classes
// final Person p = Person();
final Person sartre = Philosopher();
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 22:16
Inheriting from default and named constructors in Dart
// Inheriting from default and named constructors in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/4882c662c9cb6bb13ce358b5cffe0968
void main() {
final Van myVan = Van("Toyota", "Odyssey");
print('Color: ${myVan.color}');
print('Make: ${myVan.make}');
print('Model: ${myVan.model}');
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 20:41
Basic inheritance and polymorphism in Dart
// Basic inheritance and polymorphism in Dart (as of v2.5)
// see https://dartpad.dartlang.org/030764a47bc7599b0e1853bda75c7639
void main() {
print('');
print('------------------ Tree object, Tree Reference ----------------------');
print('');
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 19:53
Private Variables and Getters/Setters in Dart
import 'dart:math';
// Object Oriented Programming Concepts in Dart ( as ov v2.5)
// Private Variables and Getters/Setters in Dart
// see https://dartpad.dartlang.org/4624e4394c7b09ae5430764abe71d8f3
void main() {
// Default getters/setters. Though it looks like we are directly manipulating the instance variable,
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 11, 2019 04:22
Frequency Counter Pattern in Dart
// https://dartpad.dartlang.org/6f21c9137b5b14a86025aa5d7dcb7d75
// Frequency Counter pattern in Dart
void main() {
/**
* Problem Description:
*
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 8, 2019 03:18
Constructors in Dart
// Constructors in Dart (as of v2.5)
// This example can be run at https://dartpad.dartlang.org/fc100517efced76881fd0d8d37f673a8
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// Use the default constructor
final Car c = Car();
print('Car with default constructor id: ${c.id}');
print('\n\n');
@kirkdrichardson
kirkdrichardson / main.dart
Created October 8, 2019 03:00
Exception Handling in Dart
// Exception Handling in Dart (since v2.2)
// This example can be run at https://dartpad.dartlang.org/4d61e7617f5ea82b73223e75242ed368
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// 🚫 Expect Error!
// int myInt = 40 / 10;
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 8, 2019 02:52
Looping and Iteration in Dart (since v2.2)
// Looping and Iteration in Dart (since v2.2)
// No surprises here.
// This example can be run at https://dartpad.dartlang.org/880e763c99d49f238578de39d5a49bdd
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// O(n) version of n(n+1)/2 series, i.e. the sum of 1 + 2 + 3 + 4...n