Created
December 30, 2021 01:46
-
-
Save anovsiradj/d7d024c62ae5d48a3b90143d0ce9e2fc to your computer and use it in GitHub Desktop.
Konversi `Map<dynamic, dynamic>` ke `Map<String, String>`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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