Skip to content

Instantly share code, notes, and snippets.

View hotdang-ca's full-sized avatar

James Robert Perih hotdang-ca

View GitHub Profile
@hotdang-ca
hotdang-ca / dart_fold.dart
Created September 9, 2021 21:10
Sample of dart fold method
class PaymentLine {
double price;
String name;
int qty;
PaymentLine({ this.price, this.name, this.qty });
}
void main() {
List<PaymentLine> payments = <PaymentLine>[];
@hotdang-ca
hotdang-ca / distance.dart
Created November 24, 2021 16:48
Dart code to calculate distance between two lat/lng decimal coordinates
import 'dart:math';
/// Describes a distance unit for conversion
enum Unit {
miles,
kilometers,
nauticalMiles,
}
/// Describes a decimal coordinate
@hotdang-ca
hotdang-ca / null_test.dart
Created November 30, 2021 21:53
Null comparators in dart
void main() {
List<String> uninitializedList;
try {
if (uninitializedList?.length == 1) {
print('Length is equal to 1, not equal to null.');
} else {
print('Length is not equal to 1, it is equal to null');
}
@hotdang-ca
hotdang-ca / utf8conversion.dart
Created December 22, 2021 20:57
Dart UTF8 Decode
import 'dart:convert';
class Product {
String name;
Product({ required this.name });
Map<String, dynamic> toJson() {
const json = <String, dynamic>{};
json['name'] = name;
@hotdang-ca
hotdang-ca / dart_mirror.dart
Last active February 3, 2022 20:20
Basic Object name via Reflection in Dart
import 'dart:mirrors';
class User {
final String email;
final String name;
const User({required this.email, required this.name});
}
extension DeclarationMirrorExtension on DeclarationMirror {
/// Name of this object or field
@hotdang-ca
hotdang-ca / throw_rethrow.dart
Created February 27, 2022 19:18
Dart Async Throw and Rethrow
Future<void> doAThing() async {
await Future.delayed(Duration(milliseconds: 500));
try {
print('Trying a dangerous thing...');
await doADangerousThing();
} catch (e) {
print('There was a minor error: $e');
rethrow;
}
@hotdang-ca
hotdang-ca / assert_isgrouped.dart
Created March 3, 2022 14:44
Asserts a method returns expected value
void main() {
BvItem test1Item = BvItem(netSize: 1);
BvItem test2Item = BvItem(netSize: 6);
BvItem test3Item = BvItem(netSize: 7);
BvItem test4Item = BvItem(netSize: 8);
BvItem test5Item = BvItem(netSize: 14);
BvItem test6Item = BvItem(netSize: 15);
BvItem test7Item = BvItem(); // yes, null.
num? netSizeA = isGrouped(test1Item) ?? (test1Item.netSize ?? -1);
@hotdang-ca
hotdang-ca / resolutions.dart
Created March 17, 2022 09:49
Screen resolutions, in Dart
/// Describes a television screen resolution
class ResolutionSize {
/// The natural width of this resolution
final int width;
/// The natural height of this resolution
final int height;
/// A shorter technical name
final String name;
@hotdang-ca
hotdang-ca / potency_conversion.dart
Created March 22, 2022 14:59
Cannabis Potency conversation
class Cannabis {
final String uom;
final num p;
Cannabis(this.uom, this.p);
}
void main() {
List<Cannabis> product = [
Cannabis('%', 5),
@hotdang-ca
hotdang-ca / dart_reflection.dart
Last active August 16, 2022 17:30
Exploring Dart Mirrors 1
import "dart:mirrors";
class Product {
String type;
String brand;
String model;
Product(this.type, this.brand, this.model);
}