i have a list of countries with their sortname/countrycode in json format, which will be called in flutter. Is there a way i can convert all this code with their flag emojis?
For example: "sortname": "PL" will gives π΅π± (idk if you guys can see the flag or not)
{
"sortname": "PL",
"name": "Poland",
}, {
"sortname": "PT",
"name": "Portugal",
}, {
"sortname": "PR",
"name": "Puerto Rico",
}, {
"sortname": "QA",
"name": "Qatar",
}, {
"sortname": "RE",
"name": "Reunion",
}, {
"sortname": "RO",
"name": "Romania",
}, {
"sortname": "RU",
"name": "Russia",
},
Here's how I did it:
String countryCode = 'us';
String flag = countryCode.toUpperCase().replaceAllMapped(RegExp(r'[A-Z]'),
(match) => String.fromCharCode(match.group(0).codeUnitAt(0) + 127397));
print(flag);
.toUpperCase()
Make all characters uppercase
RegExp(r'[A-Z]')
Select each character with regex
.replaceAllMapped()
Get each matched character
.codeUnitAt(0)
Convert each character to a rune
+ 127397
Convert each rune to a regional indicator symbol
127397 = 127462 (π¦'s HTML code) - 65 (A's rune value).
.fromCharCode()
Convert the regional indicator symbols' values to a string (flag emoji)
you could try flutter_emoji package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With