Skip to content

Instantly share code, notes, and snippets.

@hereisderek
Last active September 13, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hereisderek/0ee5fe2ae684c320d9845bb20df07ded to your computer and use it in GitHub Desktop.
Save hereisderek/0ee5fe2ae684c320d9845bb20df07ded to your computer and use it in GitHub Desktop.
Dart language snippit
void main() {
/// maps
/// result:
/// map0: {1: 1, 2: 2}
/// map1: {1: 1, 2: 2}
/// map2: {1: 2, 2: 3}
Map<String, int> map = {'1' : 1, '2' : 2};
print('map0: $map');
final map2 = map.map((a, value) {
return MapEntry<String, String>(a, (++value).toString());
}
);
print('map1: $map');
print('map2: $map2');
/// datetime to string converter
/// result:
/// 2018-09-11T16:57:15.873Z
/// 2018-09-11 16:53:21.021Z
DateTime stringToDateTime(String s) => DateTime.parse(s);
String timeToString(DateTime t) => t.toUtc().toIso8601String();
final now = DateTime.now();
print(timeToString(now));
final str = '2018-09-11T16:53:21.021Z';
print(stringToDateTime(str).toString());
}
void main(){
List a = [A, B];
print(a.runtimeType); // List<dynamic>
print(a[0].runtimeType); // _Type
List b = EnumTypes;
// b: List<dynamic> b[0]: _Type b[1]: _Type
print("runtimeType: b: ${b.runtimeType} b[0]: ${b[0].runtimeType} b[1]: ${b[1].runtimeType}");
E1 e1 = E1.a;
print(e1 is E1); // true
final e1Str = 'a';
// T is in EnumTypes list: true
// str2Enum('a'): null
print("str2Enum('a'): ${str2Enum<E1>(e1Str)}");
}
const List EnumTypes = [E1, E2];
class A{}
class B{}
enum E1{
a, b, c
}
enum E2{
d, e, f
}
enum E3{
g, h, i
}
T str2Enum<T>(String s) {
print("T is in EnumTypes list: ${EnumTypes.contains(T)}");
final type = EnumTypes.firstWhere((t) {
final result = t == T;
print("checking t: $t, T: $T, (=)?: $result");
return result;
});
// final same = $type==$T;
print("type:$type, T:$T, type=T? ${type == T}, type.runtimeType:${type.runtimeType}");
var result;
try {
result = T.values.firstWhere((x) => x.toString().toLowerCase() == "$T.$s".toLowerCase());
print("result: $result");
} catch (e) {
print("error: $e");
}
return result;
}
@hereisderek
Copy link
Author

hereisderek commented Sep 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment