Skip to content

Instantly share code, notes, and snippets.

@anovsiradj
Created December 30, 2021 01:46
Show Gist options
  • Save anovsiradj/d7d024c62ae5d48a3b90143d0ce9e2fc to your computer and use it in GitHub Desktop.
Save anovsiradj/d7d024c62ae5d48a3b90143d0ce9e2fc to your computer and use it in GitHub Desktop.
Konversi `Map<dynamic, dynamic>` ke `Map<String, String>`
/*
* melakukan konversi `Map<dynamic, dynamic>` ke `Map<String, String>`.
* dengan ketentuan, `null` harus menjadi `` (string kosong, bukan string `'null'`).
* yang digunakan sebagai body pada http-request.
*/
void main() {
Map<dynamic, dynamic> params = {
'null': null,
'int0': 0,
'int1': 1,
'float0': 0.0,
'float1': 0.9,
'string': 'string',
};
Map<String, String> params999 =
params.map((k, v) => MapEntry(k, v == null ? '' : v.toString()));
Map<String, String> params998 =
params.map((k, v) => MapEntry(k, v?.toString() ?? ''));
print(params); // NOPE
print(params999); // GOOD
print(params998); // GOOD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment