Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Last active October 10, 2019 04:37
Show Gist options
  • Save hilfritz/db3d4cc8433d6aa4434dc0b4f2f0d8ba to your computer and use it in GitHub Desktop.
Save hilfritz/db3d4cc8433d6aa4434dc0b4f2f0d8ba to your computer and use it in GitHub Desktop.
Dart Flutter
/*
* Prints the items in the list with delay
*/
StreamSubscription<String> timerSubscription;
List<String> iterable = ["2", "1", "x"];
timerSubscription = Observable.fromIterable(timerText).concatMap((i) {
return new Observable.timer(i, Duration(seconds: 1));
}).listen((x) {
print(x); //Prints "2" , 1 sec later then prints "1", 1 sec later prints "x"
if (x == iterable[0]) {
} else if (x == iterable[1]) {
} else if (x == iterable[2]) {
}
}, onError: (x) {
timerSubscription?.cancel();
}, onDone: () {
timerSubscription?.cancel();
});
/**
* Convert Multi-layered Object to Key value pairs Map Object
*/
Map<String, dynamic> toMap(Map map, {String oldKey = ""}) {
Map<String, dynamic> retVal = Map<String, dynamic>();
map.forEach((key, value) {
if (value != null) {
if (value is int) {
String newKey = oldKey + "[$key]";
if (oldKey.isEmpty) {
newKey = key;
}
retVal.putIfAbsent(newKey, () => value);
} else if (value is String) {
String newKey = oldKey + "[$key]";
if (oldKey.isEmpty) {
newKey = key;
}
retVal.putIfAbsent(newKey, () => value);
} else if (value is Map) {
String newKey = oldKey + "[$key]";
if (oldKey.isEmpty) {
newKey = key;
}
retVal.addAll(toMap(value, oldKey: newKey));
} else if (value is List<Map>) {
int index = 0;
value.forEach((x) {
String newKey = oldKey + "[$key]";
if (oldKey.isEmpty) {
newKey = key;
}
retVal.addAll(toMap(x, oldKey: newKey + '[$index]'));
index++;
});
}
}
});
return retVal;
}
/**
* To Use:
* 1. Convert Object to JSON String -> output needs to be Map<String, dynamic> object
* 2. Pass the Map<String, dynamic> object to the toMap()
* 3.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment