Skip to content

Instantly share code, notes, and snippets.

View iAmWillShepherd's full-sized avatar

William Shepherd iAmWillShepherd

View GitHub Profile
@iAmWillShepherd
iAmWillShepherd / dart-library-prefix.md
Created January 25, 2022 01:13
Example of prefixing a library in Dart
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;

// Uses Element from lib1.
Element element1 = Element();

// Uses Element from lib2.
lib2.Element element2 = lib2.Element();
@iAmWillShepherd
iAmWillShepherd / dart-selective-imports.md
Last active December 9, 2021 17:54
Importing only part of a library in Dart
@iAmWillShepherd
iAmWillShepherd / dart-stacktrace.md
Created December 8, 2021 16:39
Dart allows you to throw any object and get a stacktrace

You can throw any valid object in Dart and still get a stack trace. Note that the stacktrace is independent from the error/exception.

Throwing a string

void main(List<String> arguments) {
  try {
    throw "I'm throwing a string, not an error!";
  } catch (e, stack) {
    print(
@iAmWillShepherd
iAmWillShepherd / dart-final-const.md
Created December 7, 2021 17:13
Comparing and constrasting Dart's const and final keywords

Use the const keyword when you want to create a compile-time constant.
Note that this value will be immutable.

const color = 'Lava Orange';
color = 'Mexico Blue';  // Error: cannot reassign a const

const colors = ['Lava Orange'];
colors.add('Lava Orange');  // Error: unsupported operation
@iAmWillShepherd
iAmWillShepherd / dart-collection-for.md
Last active December 3, 2021 00:00
Comparing adding elements to a collection using Dart's collection-for syntax vs. JavaScript

Dart

const colors = ['840420', '2898b2', '606476'];
const hexCodes = ['#000000', for (var c in colors) '#$c', '#ffffff'];

hexCodes;  // ['#000000', '#840420', '#2898b2', '#606476', `#ffffff`]

vs

@iAmWillShepherd
iAmWillShepherd / dart-spread-syntax.dart
Created November 29, 2021 23:44
Constructing lists in dart using the spread operator
const xs = [1, 2, 3];
const xsWithZero = [0, ...xs];
print(xsWithZero); // [0, 1, 2, 3]
const xsWithZeroBack = [...xs, 0]
print(xsWithZeroBack)' // [1, 2, 3, 0]
@iAmWillShepherd
iAmWillShepherd / data-tags-api-issue.md
Created November 22, 2021 22:19
I'm unable to update data tags

Issue updating data tags via API

I tried updating the tags for a device and received a success result from the API; however, upon instpecting the device in the dashboard, I noted the device was not updated.

Screenshot of dashboard

☝️ This is after executing the request several times.

Request

@iAmWillShepherd
iAmWillShepherd / try-catch-comparison.md
Created November 22, 2021 19:35
try...catch: dart vs javascript
Future<void> somethingThatCanFail() async {
  try {
    // try some stuff likely to fail
  } on SocketException catch (_) {
    print('Something happend to the internet')
  } on IOException catch (_) {
    print('Something happened to your disk');
  } catch (e) {
 print('An unexpected error occured: $e');
@iAmWillShepherd
iAmWillShepherd / dart-sets.dart
Created November 22, 2021 19:23
Use a set when you want to ensure each value in the collection is unique.
const zeroTo60 = <String, double>{
'Porsche 991.2 Carerra T': 3.5,
'Porsche 991.2 Carerra GTS': 3.4,
'Porsche 991.2 GT3': 3.0,
'Porsche 991.2 GT3': 3.6,
'Porsche 992 Turbo S': 2.2,
'Porsche 992 GT3': 2.7
}
const s = Set.from(zeroTo60.keys)
@iAmWillShepherd
iAmWillShepherd / dart-static-class.dart
Created November 22, 2021 19:19
Dart's static keywork ensure only one instance of an object can be instantiated.
class Color {
static const red = '#f00';
static const green = '#0f0';
static const blue = '#00f';
static const black = '#000';
static const white = '#fff';
}