UTF-8 Decoder with Response Modal in Flutter
Hi Coders,
Have you ever heard about UTF-8 ?
i will not gonna go deeply on that because it will the article too long and boring So you can just google it if you don't know about it.
So here in this article i am gonna show how you can decode UTF-8 character and use data getting from after decoding UTF-8 and use it with Response modal and then convert it to human readable language.
I know its looks like a long procedure but its very easy and simple
But But But very important question is that why we need to do this or know about this?
PaymentTransactionListResponse paymentTransactionListResponse=PaymentTransactionListResponse.fromJson(jsonDecode(response.body))
Here when we normally use data from api with response modal then some special characters like postérieur converted into postÄ€©rieur and why this happen So let me clear this out that
- You saved data into database and the data is : postérieur ,Here your data first encoded into UTF-8 character
- You get same data from same database and the data you get is postÄ€©rieur , your data decoded from UTF-8 characters to human readable language
But it has its own disadvantages i.e. sometimes you saved some words or data into database and here database into not local and when you again get that data using api you will get the different word/data and you can't change UTF-8 characters because it have different for every alphabet/character/words and that's why my Article is here to help you ☺
String source = Utf8Decoder().convert(response.bodyBytes);
var newSourceValue = jsonDecode(source);
PaymentTransactionListResponse paymentTransactionListResponse=PaymentTransactionListResponse.fromJson(newSourceValue)
Solution in brief:
First we decode response data i.e. the response body we get by get API but here one thing to remember is that in normal we use only response.body but here we use response.bodyBytes.
And i name that decode result as source and after UTF-8 decode the result you get will be in String.Now we get data in String But we can't directly use this in our project because there may be multiple data So it will be difficult to handle and code will looks bad So we will use Modal for that But modal always takes object as input and for that we need to convert String into modal object So we use jsonDecode(source) here.
Now at the last just put the newValue with modal as like we do normally.
If you have any doubts regarding this then feel free to ask me via just commenting below or you can DM me on any social media platform
Thank You