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 / doubles.dart
Last active May 26, 2022 16:52
Dart Double Fun
import 'dart:math';
double roundDouble(double value, double places) {
num mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
extension BetterDoubles on num {
num roundAsDoubleBetter(double places) {
final num mod = pow(10.0, places);
@hotdang-ca
hotdang-ca / dart_labels.dart
Created April 24, 2022 06:48
Dart has Labels!!
void main() {
ten:while(true) {
print('Hello world!');
continue ten;
break;
}
}
@hotdang-ca
hotdang-ca / isolates_fun.dart
Created April 24, 2022 06:47
Isolate Fun in Dart
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
void main(List<String> arguments) async {
ReceivePort receivePort = ReceivePort();
Isolate fooIsolate = await Isolate.spawn(foo, receivePort.sendPort);
Isolate barIsolate = await Isolate.spawn(bar, receivePort.sendPort);
Isolate fooBarIsolate = await Isolate.spawn(fooBar, receivePort.sendPort);
@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 / 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 / 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 / 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 / random_stream.dart
Last active March 17, 2022 10:07
Random Flutter Stream
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
void main() {
runApp(MyApp());
}
class RandomNumberStream {
final StreamController<int> _nextRandomNumberController = StreamController<int>();
@hotdang-ca
hotdang-ca / main.dart
Last active March 17, 2022 10:07
Fizzbuzzing Dart
/// Oct 23, 2020
/// Updated March 17, 2021
/// (c) 2020 James Robert Perih c/o Four And A Half Giraffes, Ltd.
void main() {
const int theStart = 1;
const int theEnd = 100;
DartFlutterOutputFactory().printIteration(
from: theStart,
@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;