Skip to content

Instantly share code, notes, and snippets.

@kirkdrichardson
kirkdrichardson / main.dart
Last active October 8, 2019 02:59
Variable and Data Types in Dart
// Variables and literals in Dart (as of v2.2)
// This example can be run at https://dartpad.dartlang.org/8e4e9266aa82674e7e0748413b3e88b2
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// NOTE - all data types in Dart are objects, so the default type is null
// TYPES OF NUMBERS - only "int" and "double" (both are 64-bit)
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 8, 2019 02:53
Control Flow in Dart (examples)
// Control Flow in Dart (since v2.2)
// This example can be run at https://dartpad.dartlang.org/dd957e22c8a763138b57b0e807e2734c
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// if / else conditional
bool happy = true;
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 8, 2019 02:59
The Difference between "final" and "const" in Dart
// This example can be run at https://dartpad.dartlang.org/c09c790d954c5d53572cb9661b36ff43
// For other examples see https://gist.github.com/kirkdrichardson
void main() {
// The Difference between "final" and "const" in Dart (as of v2.2)
// Both final and const prevent a variable from being reassigned
// (similar to how final works in Java or how const works in JavaScript).
final once = "My final var";
@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
@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 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
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 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 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 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}');