{"id":87942,"date":"2025-04-03T11:12:52","date_gmt":"2025-04-03T04:12:52","guid":{"rendered":"https:\/\/itviecblog.uptech.vn\/?p=85368"},"modified":"2025-06-17T09:14:14","modified_gmt":"2025-06-17T02:14:14","slug":"json-to-dart","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/json-to-dart\/","title":{"rendered":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#JSON_la_gi_trong_ngu_canh_cua_Dart\" >JSON l\u00e0 g\u00ec trong ng\u1eef c\u1ea3nh c\u1ee7a Dart?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Vi_sao_can_chuyen_doi_tu_JSON_sang_Dart\" >V\u00ec sao c\u1ea7n chuy\u1ec3n \u0111\u1ed5i t\u1eeb JSON sang Dart?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Cac_phuong_phap_chuyen_doi_JSON_sang_Dart\" >C\u00e1c ph\u01b0\u01a1ng ph\u00e1p chuy\u1ec3n \u0111\u1ed5i JSON sang Dart\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Su_dung_freezed_de_tao_model_Dart\" >S\u1eed d\u1ee5ng freezed \u0111\u1ec3 t\u1ea1o model Dart<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Cac_cong_cu_ho_tro_chuyen_doi_tu_JSON_sang_Dart\" >C\u00e1c c\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3 chuy\u1ec3n \u0111\u1ed5i t\u1eeb JSON sang Dart<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Luu_y_khi_lam_viec_voi_JSON_trong_Dart\" >L\u01b0u \u00fd khi l\u00e0m vi\u1ec7c v\u1edbi JSON trong Dart<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Cac_cau_hoi_thuong_gap_ve_JSON_to_Dart\" >C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 JSON to Dart<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>Trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng v\u1edbi Dart v\u00e0 Flutter, vi\u1ec7c l\u00e0m vi\u1ec7c v\u1edbi JSON l\u00e0 kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi, \u0111\u1eb7c bi\u1ec7t khi \u1ee9ng d\u1ee5ng c\u1ea7n trao \u0111\u1ed5i d\u1eef li\u1ec7u v\u1edbi API. Tuy nhi\u00ean, JSON ch\u1ec9 l\u00e0 m\u1ed9t \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u th\u00f4, c\u00f2n Dart l\u1ea1i s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 ki\u1ec3u d\u1eef li\u1ec7u c\u1ee5 th\u1ec3. \u0110i\u1ec1u n\u00e0y khi\u1ebfn vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i JSON sang Dart tr\u1edf th\u00e0nh m\u1ed9t b\u01b0\u1edbc quan tr\u1ecdng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c x\u1eed l\u00fd m\u1ed9t c\u00e1ch an to\u00e0n v\u00e0 hi\u1ec7u qu\u1ea3, tr\u00e1nh m\u1ea5t nhi\u1ec1u th\u1eddi gian x\u1eed l\u00fd l\u1ed7i ho\u1eb7c vi\u1ebft code d\u01b0 th\u1eeba.<\/strong><\/em><\/p>\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 t\u00ecm hi\u1ec3u th\u00eam:<\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c ph\u01b0\u01a1ng ph\u00e1p chuy\u1ec3n \u0111\u1ed5i JSON sang Dart<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c v\u00ed d\u1ee5 th\u1ef1c t\u1ebf<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3 chuy\u1ec3n \u0111\u1ed5i JSON sang Dart<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"JSON_la_gi_trong_ngu_canh_cua_Dart\"><\/span><b>JSON l\u00e0 g\u00ec trong ng\u1eef c\u1ea3nh c\u1ee7a Dart?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\"><a href=\"\/blog\/json-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>JSON<\/strong><\/a> (JavaScript Object Notation)<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn, \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng r\u1ed9ng r\u00e3i \u0111\u1ec3 trao \u0111\u1ed5i th\u00f4ng tin gi\u1eefa m\u00e1y ch\u1ee7 v\u00e0 \u1ee9ng d\u1ee5ng. V\u1edbi c\u00fa ph\u00e1p \u0111\u01a1n gi\u1ea3n, d\u1ec5 \u0111\u1ecdc v\u00e0 nh\u1eb9, JSON tr\u1edf th\u00e0nh m\u1ed9t l\u1ef1a ch\u1ecdn l\u00fd t\u01b0\u1edfng trong ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi l\u00e0m vi\u1ec7c v\u1edbi API.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong ng\u1eef c\u1ea3nh c\u1ee7a Flutter\/Dart, JSON \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c x\u1eed l\u00fd d\u1eef li\u1ec7u t\u1eeb backend. Tuy nhi\u00ean, do s\u1ef1 kh\u00e1c bi\u1ec7t v\u1ec1 ki\u1ec3u d\u1eef li\u1ec7u gi\u1eefa JSON v\u00e0 Dart, vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i JSON sang c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng Dart c\u1ea7n \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh nh\u1ea5t qu\u00e1n v\u00e0 t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t.<\/span><\/p>\n<blockquote><p>\u0110\u1ecdc th\u00eam: <a href=\"\/blog\/xay-dung-mobile-app-voi-dart-flutter\/\" target=\"_blank\" rel=\"noopener\"><strong>Dart Flutter l\u00e0 g\u00ec: C\u00e1ch x\u00e2y d\u1ef1ng mobile app v\u1edbi Dart trong Flutter<\/strong><\/a><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Vi_sao_can_chuyen_doi_tu_JSON_sang_Dart\"><\/span><b>V\u00ec sao c\u1ea7n chuy\u1ec3n \u0111\u1ed5i t\u1eeb JSON sang Dart?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trong ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng Flutter, vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i t\u1eeb JSON sang Dart c\u1ea7n thi\u1ebft v\u00ec:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>D\u1ec5 d\u00e0ng qu\u1ea3n l\u00fd d\u1eef li\u1ec7u<\/strong>: JSON l\u00e0 m\u1ed9t \u0111\u1ecbnh d\u1ea1ng ph\u1ed5 bi\u1ebfn, nh\u01b0ng d\u1ea1ng Map&lt;String, dynamic&gt; c\u1ee7a n\u00f3 khi\u1ebfn vi\u1ec7c truy xu\u1ea5t d\u1eef li\u1ec7u thi\u1ebfu an to\u00e0n v\u00e0 d\u1ec5 g\u00e2y l\u1ed7i. Vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i JSON th\u00e0nh c\u00e1c model Dart gi\u00fap l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u theo c\u00e1ch c\u00f3 ki\u1ec3m so\u00e1t h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>T\u0103ng c\u01b0\u1eddng ki\u1ec3m tra ki\u1ec3u d\u1eef li\u1ec7u (Type Safety)<\/strong>: Khi chuy\u1ec3n \u0111\u1ed5i JSON sang class Dart, c\u00e1c thu\u1ed9c t\u00ednh s\u1ebd c\u00f3 ki\u1ec3u d\u1eef li\u1ec7u c\u1ee5 th\u1ec3 (int, String, bool, List, v.v.), gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i runtime.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>D\u1ec5 d\u00e0ng s\u1eed d\u1ee5ng v\u00e0 b\u1ea3o tr\u00ec<\/strong>: Khi c\u00f3 m\u1ed9t model Dart, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ch\u00fang \u0111\u1ec3:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng parse d\u1eef li\u1ec7u t\u1eeb JSON m\u00e0 kh\u00f4ng c\u1ea7n vi\u1ebft nhi\u1ec1u \u0111o\u1ea1n code x\u1eed l\u00fd.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">T\u1ea1o object t\u1eeb JSON v\u00e0 chuy\u1ec3n object th\u00e0nh JSON m\u1ed9t c\u00e1ch thu\u1eadn ti\u1ec7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng m\u1edf r\u1ed9ng khi API thay \u0111\u1ed5i (ch\u1ec9 c\u1ea7n c\u1eadp nh\u1eadt model thay v\u00ec s\u1eeda nhi\u1ec1u n\u01a1i trong code).<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>H\u1ed7 tr\u1ee3 t\u1ed1t cho State Management<\/strong>: Khi l\u00e0m vi\u1ec7c v\u1edbi Provider, Riverpod, Bloc, GetX, d\u1eef li\u1ec7u th\u01b0\u1eddng c\u1ea7n \u1edf d\u1ea1ng object thay v\u00ec JSON \u0111\u1ec3 d\u1ec5 d\u00e0ng thao t\u00e1c. Chuy\u1ec3n \u0111\u1ed5i JSON sang Dart gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng c\u1eadp nh\u1eadt v\u00e0 theo d\u00f5i tr\u1ea1ng th\u00e1i c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>C\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t<\/strong>: Vi\u1ec7c truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb object nhanh h\u01a1n so v\u1edbi Map v\u00ec Dart c\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c truy c\u1eadp thu\u1ed9c t\u00ednh. Ngo\u00e0i ra, c\u00f2n gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i null nh\u1edd null safety c\u1ee7a Dart.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_phuong_phap_chuyen_doi_JSON_sang_Dart\"><\/span><b>C\u00e1c ph\u01b0\u01a1ng ph\u00e1p chuy\u1ec3n \u0111\u1ed5i JSON sang Dart\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n \u0111ang l\u00e0m vi\u1ec7c v\u1edbi JSON trong Flutter\/Dart, c\u00f3 hai ph\u01b0\u01a1ng ph\u00e1p ph\u1ed5 bi\u1ebfn \u0111\u1ec3 chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chuy\u1ec3n \u0111\u1ed5i th\u1ee7 c\u00f4ng:<\/b><span style=\"font-weight: 400;\"> Ph\u00f9 h\u1ee3p v\u1edbi JSON \u0111\u01a1n gi\u1ea3n, d\u1ec5 hi\u1ec3u nh\u01b0ng c\u00f3 th\u1ec3 g\u00e2y m\u1ea5t c\u00f4ng khi x\u1eed l\u00fd JSON ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng json_serializable:<\/b><span style=\"font-weight: 400;\"> Gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a qu\u00e1 tr\u00ecnh \u00e1nh x\u1ea1 JSON, gi\u1ea3m thi\u1ec3u l\u1ed7i v\u00e0 ti\u1ebft ki\u1ec7m th\u1eddi gian nh\u01b0ng y\u00eau c\u1ea7u thi\u1ebft l\u1eadp ban \u0111\u1ea7u.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">T\u00f9y v\u00e0o nhu c\u1ea7u d\u1ef1 \u00e1n, b\u1ea1n c\u00f3 th\u1ec3 l\u1ef1a ch\u1ecdn ph\u01b0\u01a1ng ph\u00e1p ph\u00f9 h\u1ee3p nh\u1ea5t \u0111\u1ec3 l\u00e0m vi\u1ec7c v\u1edbi JSON m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 trong Flutter\/Dart.<\/span><\/p>\n<h3><b>S\u1eed d\u1ee5ng dart:convert v\u1edbi jsonDecode()<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Dart cung c\u1ea5p th\u01b0 vi\u1ec7n <\/span><span style=\"font-weight: 400;\">dart:convert<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 gi\u00fap ch\u00fang ta ph\u00e2n t\u00edch c\u00fa ph\u00e1p JSON m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">import 'dart:convert';<\/span>\n<span style=\"font-weight: 400;\">void main() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0String jsonString = '{\"id\": 1, \"name\": \"Flutter\"}';<\/span>\n<span style=\"font-weight: 400;\"> \u00a0Map&lt;String, dynamic&gt; jsonData = jsonDecode(jsonString);<\/span>\n<span style=\"font-weight: 400;\"> \u00a0print(jsonData['name']); \/\/ Output: Flutter<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Thay v\u00ec l\u00e0m vi\u1ec7c tr\u1ef1c ti\u1ebfp v\u1edbi Map, ta c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t model Dart b\u1eb1ng tay \u0111\u1ec3 \u00e1nh x\u1ea1 d\u1eef li\u1ec7u t\u1eeb JSON:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class User {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final int id;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final String name;<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0User({required this.id, required this.name});<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0factory User.fromJson(Map&lt;String, dynamic&gt; json) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return User(<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0id: json['id'],<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0name: json['name'],<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0);<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0Map&lt;String, dynamic&gt; toJson() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0'id': id,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0'name': name,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0};<\/span>\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Sau khi \u0111\u00e3 t\u1ea1o, s\u1eed d\u1ee5ng model n\u00e0y nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">void main() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0String jsonString = '{\"id\": 1, \"name\": \"Flutter\"}';<\/span>\n<span style=\"font-weight: 400;\"> \u00a0Map&lt;String, dynamic&gt; jsonData = jsonDecode(jsonString);<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0User user = User.fromJson(jsonData);<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0print(user.name); \/\/ Output: Flutter<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><b>\u01afu \u0111i\u1ec3m:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u1ec5 hi\u1ec3u, kh\u00f4ng c\u1ea7n ph\u1ee5 thu\u1ed9c v\u00e0o package b\u00ean ngo\u00e0i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t t\u1ed1t qu\u00e1 tr\u00ecnh chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<p><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u1ec5 x\u1ea3y ra l\u1ed7i n\u1ebfu JSON ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Y\u00eau c\u1ea7u vi\u1ebft nhi\u1ec1u m\u00e3 l\u1eb7p \u0111i l\u1eb7p l\u1ea1i, \u0111\u1eb7c bi\u1ec7t v\u1edbi c\u00e1c model l\u1edbn.<\/span><\/li>\n<\/ul>\n<h3><b>Chuy\u1ec3n \u0111\u1ed5i JSON sang Dart b\u1eb1ng json_serializable<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">json_serializable<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t package h\u1ed7 tr\u1ee3 code generation, gi\u00fap t\u1ef1 \u0111\u1ed9ng t\u1ea1o ph\u01b0\u01a1ng th\u1ee9c \u00e1nh x\u1ea1 JSON sang Dart model m\u00e0 kh\u00f4ng c\u1ea7n vi\u1ebft th\u1ee7 c\u00f4ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u th\u00eam v\u00e0 ti\u1ebfn h\u00e0nh c\u00e0i \u0111\u1eb7t package n\u00e0y t\u1ea1i \u0111\u00e2y: <\/span><a href=\"https:\/\/pub.dev\/packages\/json_serializable\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/pub.dev\/packages\/json_serializable<\/span><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Sau khi c\u00e0i \u0111\u1eb7t, b\u1ea1n thi\u1ebft l\u1eadp json_serializable trong d\u1ef1 \u00e1n Flutter\/Dart b\u1eb1ng c\u00e1ch th\u00eam c\u00e1c package sau v\u00e0o <\/span><span style=\"font-weight: 400;\">pubspec.yaml<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">dependencies:<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0json_annotation: ^4.8.1<\/span>\n\n<span style=\"font-weight: 400;\">dev_dependencies:<\/span>\n<span style=\"font-weight: 400;\"> \u00a0build_runner: ^2.4.6<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0json_serializable: ^6.7.1<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ch\u1ea1y l\u1ec7nh:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">flutter pub get<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">T\u1ea1o model Dart v\u1edbi @JsonSerializable v\u00e0 ch\u1ea1y code generation:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">T\u1ea1o model User:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">import 'package:json_annotation\/json_annotation.dart';<\/span>\n\n<span style=\"font-weight: 400;\">part 'user.g.dart';<\/span>\n\n<span style=\"font-weight: 400;\">@JsonSerializable()<\/span>\n<span style=\"font-weight: 400;\">class User {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final int id;<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0final String name;<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0User({required this.id, required this.name});<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0factory User.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$UserFromJson(json);<\/span>\n<span style=\"font-weight: 400;\"> \u00a0Map&lt;String, dynamic&gt; toJson() =&gt; _$UserToJson(this);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<ul>\n<li><span style=\"font-weight: 400;\">Ch\u1ea1y l\u1ec7nh \u0111\u1ec3 t\u1ea1o file .g.dart:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">flutter pub run build_runner build<\/span><\/pre>\n<ul>\n<li><span style=\"font-weight: 400;\">B\u00e2y gi\u1edd b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng model n\u00e0y nh\u01b0 sau:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">void main() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0String jsonString = '{\"id\": 1, \"name\": \"Flutter\"}';<\/span>\n<span style=\"font-weight: 400;\"> \u00a0Map&lt;String, dynamic&gt; jsonData = jsonDecode(jsonString);<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0User user = User.fromJson(jsonData);<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0print(user.name); \/\/ Output: Flutter<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><b>\u01afu \u0111i\u1ec3m:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng h\u00f3a vi\u1ec7c \u00e1nh x\u1ea1 JSON, gi\u00fap gi\u1ea3m m\u00e3 l\u1eb7p \u0111i l\u1eb7p l\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ea1n ch\u1ebf l\u1ed7i sai khi x\u1eed l\u00fd JSON ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 d\u1ec5 d\u00e0ng c\u1eadp nh\u1eadt model khi thay \u0111\u1ed5i JSON.<\/span><\/li>\n<\/ul>\n<p><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea7n thi\u1ebft l\u1eadp ban \u0111\u1ea7u v\u00e0 ch\u1ea1y <\/span><span style=\"font-weight: 400;\">build_runner<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u1ee5 thu\u1ed9c v\u00e0o package b\u00ean ngo\u00e0i.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Su_dung_freezed_de_tao_model_Dart\"><\/span><b>S\u1eed d\u1ee5ng freezed \u0111\u1ec3 t\u1ea1o model Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a href=\"https:\/\/pub.dev\/packages\/freezed\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">freezed<\/span><\/a> <span style=\"font-weight: 400;\">l\u00e0 m\u1ed9t package m\u1ea1nh m\u1ebd gi\u00fap t\u1ea1o immutable data classes, h\u1ed7 tr\u1ee3 serialization v\u00e0 copyWith d\u1ec5 d\u00e0ng. \u0110\u00e2y l\u00e0 m\u1ed9t gi\u1ea3i ph\u00e1p hi\u1ec7n \u0111\u1ea1i thay th\u1ebf cho vi\u1ec7c vi\u1ebft model th\u1ee7 c\u00f4ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng freezed gi\u00fap cho vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i JSON th\u00e0nh Dart (v\u00e0 ng\u01b0\u1ee3c l\u1ea1i) \u0111\u01b0\u1ee3c bi\u1ebft \u0111\u1ebfn l\u00e0 m\u1ed9t nhu c\u1ea7u ph\u1ed5 bi\u1ebfn khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u t\u1eeb API tr\u1edf n\u00ean t\u1ef1 \u0111\u1ed9ng, d\u1ec5 d\u00e0ng v\u00e0 ch\u00ednh x\u00e1c h\u01a1n\u00a0 v\u1edbi c\u00e1c \u0111i\u1ec3m m\u1ea1nh sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>\u0110\u1eb7c \u0111i\u1ec3m\u00a0<\/strong><\/td>\n<td><strong>L\u1ee3i \u00edch c\u1ee7a Freezed<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Chuy\u1ec3n \u0111\u1ed5i JSON d\u1ec5 d\u00e0ng h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng t\u1ea1o <\/span><span style=\"font-weight: 400;\">fromJson()<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">toJson()<\/span><span style=\"font-weight: 400;\"> v\u1edbi <\/span><span style=\"font-weight: 400;\">json_serializable<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">B\u1ea5t bi\u1ebfn (Immutable)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i ngo\u00e0i \u00fd mu\u1ed1n, an to\u00e0n h\u01a1n.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 JSON ph\u1ee9c t\u1ea1p<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng \u00e1nh x\u1ea1 JSON l\u1ed3ng nhau v\u00e0o model Dart.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 Union Types<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng x\u1eed l\u00fd nhi\u1ec1u tr\u1ea1ng th\u00e1i d\u1eef li\u1ec7u trong API.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Gi\u1ea3m boilerplate code<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng sinh m\u00e3, gi\u00fap code ng\u1eafn g\u1ecdn, d\u1ec5 b\u1ea3o tr\u00ec.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">An to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u<\/span><\/td>\n<td><span style=\"font-weight: 400;\">H\u1ea1n ch\u1ebf l\u1ed7i <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">, tr\u00e1nh l\u1ed7i runtime.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">T\u00edch h\u1ee3p t\u1ed1t<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng m\u01b0\u1ee3t v\u1edbi Bloc, Provider, Riverpod.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 thi\u1ebft l\u1eadp v\u00e0 s\u1eed d\u1ee5ng freezed v\u1edbi c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u00eam c\u00e1c package v\u00e0o pubspec.yaml:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">dependencies:<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0freezed_annotation: ^2.4.1<\/span>\n\n<span style=\"font-weight: 400;\">dev_dependencies:<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0build_runner: ^2.4.6<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0freezed: ^2.4.5<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u1ea1y l\u1ec7nh:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">flutter pub get<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ea1o model User:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">import 'package:freezed_annotation\/freezed_annotation.dart';<\/span>\n\n<span style=\"font-weight: 400;\">part 'user.freezed.dart';<\/span>\n<span style=\"font-weight: 400;\">part 'user.g.dart';<\/span>\n\n<span style=\"font-weight: 400;\">@freezed<\/span>\n<span style=\"font-weight: 400;\">class User with _$User {<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0factory User({<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0required int id,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0required String name,<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0}) = _User;<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0factory User.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$UserFromJson(json);<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u1ea1y l\u1ec7nh \u0111\u1ec3 t\u1ea1o code:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">flutter pub run build_runner build<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u00e2y gi\u1edd b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng model User t\u01b0\u01a1ng t\u1ef1 nh\u01b0 v\u1edbi json_serializable nh\u01b0ng c\u00f3 th\u00eam nhi\u1ec1u t\u00ednh n\u0103ng n\u00e2ng cao.<\/span><\/p>\n<h3><b>So s\u00e1nh freezed v\u1edbi json_serializable<\/b><\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Ti\u00eau ch\u00ed\u00a0<\/b><\/td>\n<td><b>json_serializable<\/b><\/td>\n<td><b>freezed<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng t\u1ea1p model<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng immutable\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 copyWith<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 Union types\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u0110\u1ed9 ph\u1ee9c t\u1ea1p\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Trung b\u00ecnh\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Cao\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>Khi n\u00e0o n\u00ean d\u00f9ng freezed?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n c\u00f3 c\u00e1c model immutable.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi c\u1ea7n h\u1ed7 tr\u1ee3 copyWith, equality t\u1ed1t h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi mu\u1ed1n s\u1eed d\u1ee5ng Union types.<\/span><\/li>\n<\/ul>\n<h3><b>Khi n\u00e0o n\u00ean d\u00f9ng json_serializable?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi ch\u1ec9 c\u1ea7n \u00e1nh x\u1ea1 JSON \u0111\u01a1n gi\u1ea3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi kh\u00f4ng c\u1ea7n c\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao nh\u01b0 Union types hay copyWith.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cong_cu_ho_tro_chuyen_doi_tu_JSON_sang_Dart\"><\/span><b>C\u00e1c c\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3 chuy\u1ec3n \u0111\u1ed5i t\u1eeb JSON sang Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c t\u1ea1o c\u00e1c model Dart t\u1eeb JSON b\u1eb1ng tay c\u00f3 th\u1ec3 t\u1ed1n th\u1eddi gian v\u00e0 d\u1ec5 m\u1eafc l\u1ed7i. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 c\u00f4ng c\u1ee5 ph\u1ed5 bi\u1ebfn gi\u00fap t\u1ef1 \u0111\u1ed9ng t\u1ea1o class t\u1eeb JSON, gi\u00fap ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 gi\u1ea3m l\u1ed7i trong qu\u00e1 tr\u00ecnh vi\u1ebft model:<\/span><\/p>\n<h3><b>JSON to Dart<\/b><b>: <a href=\"https:\/\/javiercbk.github.io\/json_to_dart\/\" target=\"_blank\" rel=\"noopener\">C\u00f4ng c\u1ee5 tr\u1ef1c tuy\u1ebfn gi\u00fap t\u1ea1o model Dart t\u1eeb JSON<\/a><\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>\u01afu \u0111i\u1ec3m<\/strong>: Giao di\u1ec7n \u0111\u01a1n gi\u1ea3n, d\u1ec5 s\u1eed d\u1ee5ng cho vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i nhanh ch\u00f3ng.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Nh\u01b0\u1ee3c \u0111i\u1ec3m<\/strong>:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Thi\u1ebfu h\u1ed7 tr\u1ee3 null-safety, khi\u1ebfn m\u00e3 ngu\u1ed3n kh\u00f4ng an to\u00e0n trong m\u00f4i tr\u01b0\u1eddng Dart hi\u1ec7n \u0111\u1ea1i.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">T\u1ea1o getters v\u00e0 setters kh\u00f4ng c\u1ea7n thi\u1ebft khi c\u00e1c tr\u01b0\u1eddng \u0111\u01b0\u1ee3c \u0111\u1eb7t l\u00e0 private, l\u00e0m t\u0103ng \u0111\u1ed9 ph\u1ee9c t\u1ea1p c\u1ee7a m\u00e3.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Kh\u00f4ng nh\u1eadn di\u1ec7n \u0111\u00fang ki\u1ec3u d\u1eef li\u1ec7u m\u1ea3ng, v\u00ed d\u1ee5 nh\u01b0 nh\u1eadn di\u1ec7n List&lt;int&gt; th\u00e0nh Object&lt;int&gt;, g\u00e2y ra l\u1ed7i khi bi\u00ean d\u1ecbch.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Thi\u1ebfu th\u00f4ng tin v\u1ec1 ngu\u1ed3n g\u1ed1c v\u00e0 h\u1ed7 tr\u1ee3, khi\u1ebfn ng\u01b0\u1eddi d\u00f9ng kh\u00f3 tin t\u01b0\u1edfng v\u00e0 s\u1eed d\u1ee5ng trong d\u1ef1 \u00e1n th\u1ef1c t\u1ebf.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a href=\"https:\/\/ashamp.github.io\/jsonToDartModel\/\" target=\"_blank\" rel=\"noopener\"><b>Dart Data Class Generator<\/b><\/a><b>: C\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3 chuy\u1ec3n \u0111\u1ed5i v\u1edbi nhi\u1ec1u t\u00f9y ch\u1ec9nh<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>\u01afu \u0111i\u1ec3m<\/strong>:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Cung c\u1ea5p nhi\u1ec1u t\u00f9y ch\u1ecdn t\u00f9y ch\u1ec9nh cho m\u00e3 ngu\u1ed3n \u0111\u01b0\u1ee3c t\u1ea1o ra, gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean \u0111i\u1ec1u ch\u1ec9nh theo nhu c\u1ea7u c\u1ee5 th\u1ec3.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">M\u00e3 ngu\u1ed3n m\u1edf v\u00e0 c\u00f3 s\u1eb5n tr\u00ean GitHub, gi\u00fap c\u1ed9ng \u0111\u1ed3ng d\u1ec5 d\u00e0ng \u0111\u00f3ng g\u00f3p v\u00e0 c\u1ea3i thi\u1ec7n.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 null-safety, ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c phi\u00ean b\u1ea3n Dart hi\u1ec7n \u0111\u1ea1i<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Nh\u01b0\u1ee3c \u0111i\u1ec3m<\/strong>:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Ch\u00e8n c\u00e1c \u0111o\u1ea1n JSON d\u01b0\u1edbi d\u1ea1ng ch\u00fa th\u00edch trong m\u00e3 ngu\u1ed3n, khi\u1ebfn m\u00e3 d\u00e0i d\u00f2ng v\u00e0 thi\u1ebfu t\u00ednh r\u00f5 r\u00e0ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">X\u1eed l\u00fd m\u1ea3ng ph\u1ee9c t\u1ea1p v\u00e0 \u0111\u00f4i khi t\u1ea1o ra c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng tr\u00f9ng l\u1eb7p v\u1edbi t\u00ean kh\u00e1c nhau, g\u00e2y nh\u1ea7m l\u1eabn v\u00e0 kh\u00f3 b\u1ea3o tr\u00ec.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Phong c\u00e1ch m\u00e3 ngu\u1ed3n kh\u00f4ng nh\u1ea5t qu\u00e1n, \u0111\u00f2i h\u1ecfi l\u1eadp tr\u00ecnh vi\u00ean ph\u1ea3i ch\u1ec9nh s\u1eeda l\u1ea1i \u0111\u1ec3 ph\u00f9 h\u1ee3p v\u1edbi ti\u00eau chu\u1ea9n d\u1ef1 \u00e1n.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a href=\"https:\/\/app.quicktype.io\/\" target=\"_blank\" rel=\"noopener\"><b>Quicktype<\/b><\/a><b>: C\u00f4ng c\u1ee5 gi\u00fap chuy\u1ec3n \u0111\u1ed5i JSON sang nhi\u1ec1u ng\u00f4n ng\u1eef<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>\u01afu \u0111i\u1ec3m<\/strong>:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 \u0111a ng\u00f4n ng\u1eef, bao g\u1ed3m c\u1ea3 Dart, gi\u00fap chuy\u1ec3n \u0111\u1ed5i JSON sang nhi\u1ec1u ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c nhau.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Cung c\u1ea5p c\u00e1c t\u00f9y ch\u1ecdn t\u00f9y ch\u1ec9nh cho l\u1edbp \u0111\u01b0\u1ee3c t\u1ea1o ra, gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean \u0111i\u1ec1u ch\u1ec9nh theo nhu c\u1ea7u c\u1ee5 th\u1ec3.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u00f3 s\u1eb5n ti\u1ec7n \u00edch m\u1edf r\u1ed9ng cho Visual Studio Code, gi\u00fap t\u00edch h\u1ee3p tr\u1ef1c ti\u1ebfp v\u00e0o m\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n..<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Nh\u01b0\u1ee3c \u0111i\u1ec3m<\/strong>:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Kh\u00f4ng s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">required<\/span><span style=\"font-weight: 400;\"> m\u00e0 thay b\u1eb1ng ch\u00fa th\u00edch <\/span><span style=\"font-weight: 400;\">@required<\/span><span style=\"font-weight: 400;\">, kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi chu\u1ea9n hi\u1ec7n t\u1ea1i c\u1ee7a Dart.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">M\u00e3 ngu\u1ed3n c\u1ea7n \u0111\u01b0\u1ee3c ch\u1ec9nh s\u1eeda th\u1ee7 c\u00f4ng \u0111\u1ec3 ph\u00f9 h\u1ee3p v\u1edbi ti\u00eau chu\u1ea9n hi\u1ec7n t\u1ea1i, g\u00e2y m\u1ea5t th\u1eddi gian v\u00e0 d\u1ec5 ph\u00e1t sinh l\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Kh\u00f4ng h\u1ed7 tr\u1ee3 null-safety v\u00e0 c\u00e1c tr\u01b0\u1eddng kh\u00f4ng \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, d\u1eabn \u0111\u1ebfn m\u00e3 ngu\u1ed3n kh\u00f4ng an to\u00e0n v\u00e0 kh\u00f4ng t\u1ed1i \u01b0u.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><a href=\"https:\/\/jsonformatter.org\/\" target=\"_blank\" rel=\"noopener\"><b>JSON formatter<\/b><\/a><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>\u01afu \u0111i\u1ec3m<\/strong>: Chuy\u1ec3n \u0111\u1ed5i c\u1ea5u tr\u00fac JSON th\u00e0nh l\u1edbp Dart c\u01a1 b\u1ea3n m\u1ed9t c\u00e1ch nhanh ch\u00f3ng.\u200b<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Nh\u01b0\u1ee3c \u0111i\u1ec3m<\/strong>: Thi\u1ebfu c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">fromJson<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">toJson<\/span><span style=\"font-weight: 400;\">, l\u00e0m gi\u1ea3m t\u00ednh h\u1eefu d\u1ee5ng c\u1ee7a m\u00e3 ngu\u1ed3n \u0111\u01b0\u1ee3c t\u1ea1o ra.\u200b<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Luu_y_khi_lam_viec_voi_JSON_trong_Dart\"><\/span><b>L\u01b0u \u00fd khi l\u00e0m vi\u1ec7c v\u1edbi JSON trong Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Khi l\u00e0m vi\u1ec7c v\u1edbi JSON trong Dart, b\u1ea1n c\u1ea7n ch\u00fa \u00fd nh\u1eefng \u0111i\u1ec3m sau:<\/span><\/p>\n<h3><b>X\u1eed l\u00fd tr\u01b0\u1eddng h\u1ee3p JSON c\u00f3 th\u1ec3 null ho\u1eb7c thi\u1ebfu d\u1eef li\u1ec7u<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">JSON t\u1eeb API c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u1ea7y \u0111\u1ee7 ho\u1eb7c ch\u1ee9a gi\u00e1 tr\u1ecb null. Do \u0111\u00f3, c\u1ea7n ki\u1ec3m tra v\u00e0 x\u1eed l\u00fd h\u1ee3p l\u00fd \u0111\u1ec3 tr\u00e1nh l\u1ed7i runtime.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class User {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final String name;<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0final int? age; \/\/ C\u00f3 th\u1ec3 null<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0User({required this.name, this.age});<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0factory User.fromJson(Map&lt;String, dynamic&gt; json) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return User(<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0name: json['name'] ?? 'Unknown',<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0age: json['age'] as int?,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0);<\/span>\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>S\u1eed d\u1ee5ng <\/b><b>required<\/b><b> v\u00e0 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh trong model<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Dart 2.12+ h\u1ed7 tr\u1ee3 null safety, do \u0111\u00f3, s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">required<\/span><span style=\"font-weight: 400;\"> gi\u00fap \u0111\u1ea3m b\u1ea3o c\u00e1c gi\u00e1 tr\u1ecb quan tr\u1ecdng kh\u00f4ng b\u1ecb null.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class Product {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final String id;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final String name;<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0final double price;<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0Product({<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0required this.id,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0required this.name,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0this.price = 0.0, \/\/ Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh<\/span>\n<span style=\"font-weight: 400;\"> \u00a0});<\/span>\n<span style=\"font-weight: 400;\">}<\/span><b><\/b><\/pre>\n<h3><b>Hi\u1ec7u su\u1ea5t v\u00e0 t\u1ed1i \u01b0u h\u00f3a khi parse JSON trong Flutter<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Parse JSON c\u00f3 th\u1ec3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn hi\u1ec7u su\u1ea5t, \u0111\u1eb7c bi\u1ec7t khi x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn. M\u1ed9t s\u1ed1 c\u00e1ch t\u1ed1i \u01b0u:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">const factory<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">@immutable<\/span><span style=\"font-weight: 400;\">: Gi\u00fap tr\u00e1nh vi\u1ec7c t\u1ea1o nhi\u1ec1u instance kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">compute()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd JSON tr\u00ean isolate kh\u00e1c:<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">import 'dart:convert';<\/span>\n<span style=\"font-weight: 400;\">import 'package:flutter\/foundation.dart';<\/span>\n\n<span style=\"font-weight: 400;\">Future&lt;User&gt; parseUser(String jsonString) async {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return compute(_parseUser, jsonString);<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">User _parseUser(String jsonString) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0final Map&lt;String, dynamic&gt; json = jsonDecode(jsonString);<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return User.fromJson(json);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">json_serializable<\/span><span style=\"font-weight: 400;\"> thay v\u00ec parse th\u1ee7 c\u00f4ng: Gi\u00fap parse nhanh h\u01a1n v\u00e0 tr\u00e1nh l\u1ed7i khi c\u1eadp nh\u1eadt model.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_thuong_gap_ve_JSON_to_Dart\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 JSON to Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>Khi n\u00e0o n\u00ean d\u00f9ng <\/b><b>json_serializable<\/b><b> thay v\u00ec vi\u1ebft tay <\/b><b>fromJson()<\/b><b> v\u00e0 <\/b><b>toJson()<\/b><b>?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi b\u1ea1n l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u JSON ph\u1ee9c t\u1ea1p ho\u1eb7c nhi\u1ec1u l\u1edbp, <\/span><span style=\"font-weight: 400;\">json_serializable<\/span><span style=\"font-weight: 400;\"> gi\u00fap t\u1ef1 \u0111\u1ed9ng t\u1ea1o code chuy\u1ec3n \u0111\u1ed5i JSON, gi\u1ea3m l\u1ed7i sai v\u00e0 ti\u1ebft ki\u1ec7m th\u1eddi gian.<\/span><\/p>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 x\u1eed l\u00fd gi\u00e1 tr\u1ecb null trong JSON khi parse?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 x\u1eed l\u00fd gi\u00e1 tr\u1ecb null trong JSON khi parse, d\u00f9ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">??<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1eb7t gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">factory User.fromJson(Map&lt;String, dynamic&gt; json) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return User(<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0name: json['name'] ?? 'Unknown',<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0age: json['age'] ?? 0,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 x\u1eed l\u00fd ki\u1ec3u d\u1eef li\u1ec7u kh\u00f4ng kh\u1edbp trong JSON?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c \u00e9p ki\u1ec3u an to\u00e0n (<\/span><span style=\"font-weight: 400;\">as<\/span><span style=\"font-weight: 400;\">) \u0111\u1ec3 tr\u00e1nh l\u1ed7i runtime:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">factory User.fromJson(Map&lt;String, dynamic&gt; json) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return User(<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0name: json['name'] as String? ?? 'Unknown',<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0age: json['age'] is int ? json['age'] : int.tryParse(json['age'].toString()) ?? 0,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0);<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><b>T\u1ed5ng k\u1ebft<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Chuy\u1ec3n \u0111\u1ed5i JSON sang Dart l\u00e0 b\u01b0\u1edbc quan tr\u1ecdng trong vi\u1ec7c x\u00e2y d\u1ef1ng \u1ee9ng d\u1ee5ng Flutter. Vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 h\u1ed7 tr\u1ee3 nh\u01b0 JSON to Dart, Dart Data Class Generator, v\u00e0 c\u00e1c package nh\u01b0 json_serializable gi\u00fap ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 gi\u1ea3m l\u1ed7i.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng v\u1edbi Dart v\u00e0 Flutter, vi\u1ec7c l\u00e0m vi\u1ec7c v\u1edbi JSON l\u00e0 kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi, \u0111\u1eb7c bi\u1ec7t khi \u1ee9ng d\u1ee5ng c\u1ea7n trao \u0111\u1ed5i d\u1eef li\u1ec7u v\u1edbi API. Tuy nhi\u00ean, JSON ch\u1ec9 l\u00e0 m\u1ed9t \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u th\u00f4, c\u00f2n Dart l\u1ea1i s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 ki\u1ec3u d\u1eef [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":87318,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-87942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3 - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u00ecm hi\u1ec3u c\u00e1c c\u00e1ch chuy\u1ec3n \u0111\u1ed5i JSON sang Dart - t\u1eeb th\u1ee7 c\u00f4ng \u0111\u1ebfn d\u00f9ng package. V\u00e0 kh\u00e1m ph\u00e1 c\u00e1c c\u00f4ng c\u1ee5 gi\u00fap b\u1ea1n thao t\u00e1c hi\u1ec7u qu\u1ea3 h\u01a1n!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/json-to-dart\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3\" \/>\n<meta property=\"og:description\" content=\"Trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng v\u1edbi Dart v\u00e0 Flutter, vi\u1ec7c l\u00e0m vi\u1ec7c v\u1edbi JSON l\u00e0 kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi, \u0111\u1eb7c bi\u1ec7t khi \u1ee9ng d\u1ee5ng c\u1ea7n trao \u0111\u1ed5i d\u1eef li\u1ec7u v\u1edbi\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/json-to-dart\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-03T04:12:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-17T02:14:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"337\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Tien Tran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tien Tran\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3 - ITviec Blog","description":"T\u00ecm hi\u1ec3u c\u00e1c c\u00e1ch chuy\u1ec3n \u0111\u1ed5i JSON sang Dart - t\u1eeb th\u1ee7 c\u00f4ng \u0111\u1ebfn d\u00f9ng package. V\u00e0 kh\u00e1m ph\u00e1 c\u00e1c c\u00f4ng c\u1ee5 gi\u00fap b\u1ea1n thao t\u00e1c hi\u1ec7u qu\u1ea3 h\u01a1n!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/json-to-dart\/","og_locale":"vi_VN","og_type":"article","og_title":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3","og_description":"Trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng v\u1edbi Dart v\u00e0 Flutter, vi\u1ec7c l\u00e0m vi\u1ec7c v\u1edbi JSON l\u00e0 kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi, \u0111\u1eb7c bi\u1ec7t khi \u1ee9ng d\u1ee5ng c\u1ea7n trao \u0111\u1ed5i d\u1eef li\u1ec7u v\u1edbi","og_url":"https:\/\/itviec.com\/blog\/json-to-dart\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-04-03T04:12:52+00:00","article_modified_time":"2025-06-17T02:14:14+00:00","og_image":[{"width":640,"height":337,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png","type":"image\/png"}],"author":"Tien Tran","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Tien Tran","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"12 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/"},"author":{"name":"Tien Tran","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298"},"headline":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3","datePublished":"2025-04-03T04:12:52+00:00","dateModified":"2025-06-17T02:14:14+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/"},"wordCount":3075,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/json-to-dart\/","url":"https:\/\/itviec.com\/blog\/json-to-dart\/","name":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3 - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png","datePublished":"2025-04-03T04:12:52+00:00","dateModified":"2025-06-17T02:14:14+00:00","description":"T\u00ecm hi\u1ec3u c\u00e1c c\u00e1ch chuy\u1ec3n \u0111\u1ed5i JSON sang Dart - t\u1eeb th\u1ee7 c\u00f4ng \u0111\u1ebfn d\u00f9ng package. V\u00e0 kh\u00e1m ph\u00e1 c\u00e1c c\u00f4ng c\u1ee5 gi\u00fap b\u1ea1n thao t\u00e1c hi\u1ec7u qu\u1ea3 h\u01a1n!","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/json-to-dart\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/json-to-dart-vippro-scaled.png","width":640,"height":337,"caption":"json to dart - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/json-to-dart\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"JSON to Dart: Ph\u01b0\u01a1ng ph\u00e1p v\u00e0 c\u00f4ng c\u1ee5 chuy\u1ec3n \u0111\u1ed5i hi\u1ec7u qu\u1ea3"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298","name":"Tien Tran","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","caption":"Tien Tran"},"url":"https:\/\/itviec.com\/blog\/author\/tien-tran\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/87942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/203"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=87942"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/87942\/revisions"}],"predecessor-version":[{"id":88166,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/87942\/revisions\/88166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/87318"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=87942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=87942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=87942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}