{"id":86362,"date":"2025-04-26T18:11:09","date_gmt":"2025-04-26T11:11:09","guid":{"rendered":"https:\/\/itviec1.uptech.vn\/?p=86362"},"modified":"2025-04-26T18:11:09","modified_gmt":"2025-04-26T11:11:09","slug":"so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/","title":{"rendered":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart"},"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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#Lap_trinh_synchronous_trong_Dart\" >L\u1eadp tr\u00ecnh synchronous trong 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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#Lap_trinh_asynchronous_trong_Dart\" >L\u1eadp tr\u00ecnh asynchronous trong 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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#So_sanh_lap_trinh_synchronous_va_asynchronous_trong_Dart\" >So s\u00e1nh l\u1eadp tr\u00ecnh synchronous v\u00e0 asynchronous trong Dart<\/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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#Khi_nao_nen_su_dung_dong_bo_va_khi_nao_nen_su_dung_bat_dong_bo_trong_Dart\" >Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng \u0111\u1ed3ng b\u1ed9 v\u00e0 khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong 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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#Cac_cau_hoi_thuong_gap_ve_so_sanh_lap_trinh_asynchronous_va_synchronous_trong_Dart\" >C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong 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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>B\u1ea1n \u0111\u00e3 bao gi\u1edd t\u1ef1 h\u1ecfi t\u1ea1i sao m\u1ed9t s\u1ed1 \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng m\u01b0\u1ee3t m\u00e0 ngay c\u1ea3 khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1eb7ng, trong khi nh\u1eefng \u1ee9ng d\u1ee5ng kh\u00e1c l\u1ea1i c\u00f3 v\u1ebb &#8220;\u0111\u1ee9ng h\u00ecnh&#8221; v\u00e0 ch\u1eadm ch\u1ea1p? B\u00ed m\u1eadt n\u1eb1m \u1edf c\u00e1ch ch\u00fang qu\u1ea3n l\u00fd c\u00e1c t\u00e1c v\u1ee5, \u0111\u1eb7c bi\u1ec7t l\u00e0 s\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa l\u1eadp tr\u00ecnh synchronous (\u0111\u1ed3ng b\u1ed9) v\u00e0 asynchronous (b\u1ea5t \u0111\u1ed3ng b\u1ed9). B\u00e0i vi\u1ebft n\u00e0y s\u1ebd \u0111i s\u00e2u v\u00e0o so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart theo t\u1eebng ng\u1eef c\u1ea3nh, gi\u00fap b\u1ea1n \u0111\u01b0a ra nh\u1eefng l\u1ef1a ch\u1ecdn quan tr\u1ecdng \u0111\u1ed1i v\u1edbi hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng Dart \u1ea3nh h\u01b0\u1edfng tr\u1ef1c ti\u1ebfp \u0111\u1ebfn tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng khi ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng.<\/strong><\/em><\/p>\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u th\u00eam v\u1ec1:<\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1eadp tr\u00ecnh synchronous (\u0111\u1ed3ng b\u1ed9) v\u00e0 (asynchronous) b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong Dart\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">So s\u00e1nh chi ti\u1ebft hai k\u1ef9 thu\u1eadt l\u1eadp tr\u00ecnh n\u00e0y (c\u00f3 code v\u00ed d\u1ee5)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi n\u00e0o d\u00f9ng synchronous ho\u1eb7c asynchronous?<\/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 v\u1ec1 hai k\u1ef9 thu\u1eadt n\u00e0y<\/span><\/li>\n<\/ul>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/dart-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Dart l\u00e0 g\u00ec? C\u00fa ph\u00e1p, \u0110\u1eb7c \u0111i\u1ec3m, \u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a Dart<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Lap_trinh_synchronous_trong_Dart\"><\/span><b>L\u1eadp tr\u00ecnh synchronous trong Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">H\u00e3y t\u01b0\u1edfng t\u01b0\u1ee3ng b\u1ea1n \u0111ang \u0111\u1ecdc m\u1ed9t cu\u1ed1n s\u00e1ch \u2013 b\u1ea1n \u0111\u1ecdc t\u1eebng trang theo th\u1ee9 t\u1ef1, kh\u00f4ng th\u1ec3 \u0111\u1ecdc trang sau khi ch\u01b0a ho\u00e0n th\u00e0nh trang tr\u01b0\u1edbc. T\u01b0\u01a1ng t\u1ef1, trong l\u1eadp tr\u00ecnh synchronous (hay l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9), c\u00e1c t\u00e1c v\u1ee5 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n m\u1ed9t c\u00e1ch tu\u1ea7n t\u1ef1, t\u1eebng b\u01b0\u1edbc m\u1ed9t. M\u1ed7i khi m\u1ed9t t\u00e1c v\u1ee5 b\u1eaft \u0111\u1ea7u, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd \u0111\u1ee3i cho t\u00e1c v\u1ee5 \u0111\u00f3 ho\u00e0n th\u00e0nh r\u1ed3i m\u1edbi chuy\u1ec3n sang t\u00e1c v\u1ee5 ti\u1ebfp theo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i c\u1ee7a l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 l\u00e0 <\/span><b>&#8220;blocking&#8221;<\/b><span style=\"font-weight: 400;\"> (ch\u1eb7n). Khi m\u1ed9t h\u00e0m \u0111\u1ed3ng b\u1ed9 \u0111\u01b0\u1ee3c g\u1ecdi, lu\u1ed3ng th\u1ef1c thi hi\u1ec7n t\u1ea1i s\u1ebd b\u1ecb &#8220;ch\u1eb7n l\u1ea1i&#8221; cho \u0111\u1ebfn khi h\u00e0m \u0111\u00f3 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3. \u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 kh\u00f4ng c\u00f3 b\u1ea5t k\u1ef3 \u0111o\u1ea1n m\u00e3 n\u00e0o kh\u00e1c trong c\u00f9ng m\u1ed9t lu\u1ed3ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c th\u1ef1c thi trong th\u1eddi gian ch\u1edd \u0111\u1ee3i n\u00e0y.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">H\u00e3y xem x\u00e9t m\u1ed9t v\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n trong Dart:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">void main() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('B\u1eaft \u0111\u1ea7u');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0String result = fetchData();<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('D\u1eef li\u1ec7u \u0111\u00e3 nh\u1eadn: $result');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('K\u1ebft th\u00fac');<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">String fetchData() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ Gi\u1ea3 l\u1eadp m\u1ed9t t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian (v\u00ed d\u1ee5: \u0111\u1ecdc file)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0for (int i = 0; i &lt; 5; i++) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('\u0110ang x\u1eed l\u00fd...');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ Gi\u1ea3 l\u1eadp th\u1eddi gian x\u1eed l\u00fd<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0sleep(Duration(seconds: 1));<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0return 'D\u1eef li\u1ec7u th\u00e0nh c\u00f4ng';<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">void sleep(Duration duration) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ H\u00e0m sleep \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 gi\u1ea3 l\u1eadp \u0111\u1ed9 tr\u1ec5<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0final stopTime = DateTime.now().add(duration);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0while (DateTime.now().isBefore(stopTime));<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 n\u00e0y, khi h\u00e0m <\/span><span style=\"font-weight: 400;\">fetchData()<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c g\u1ecdi, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd d\u1eebng l\u1ea1i v\u00e0 th\u1ef1c hi\u1ec7n v\u00f2ng l\u1eb7p gi\u1ea3 l\u1eadp t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian. Ch\u1ec9 khi v\u00f2ng l\u1eb7p n\u00e0y ho\u00e0n th\u00e0nh v\u00e0 h\u00e0m <\/span><span style=\"font-weight: 400;\">fetchData()<\/span><span style=\"font-weight: 400;\"> tr\u1ea3 v\u1ec1, d\u00f2ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">print(&#8216;D\u1eef li\u1ec7u \u0111\u00e3 nh\u1eadn: $result&#8217;);<\/span><span style=\"font-weight: 400;\"> m\u1edbi \u0111\u01b0\u1ee3c th\u1ef1c thi. Sau \u0111\u00f3, ch\u01b0\u01a1ng tr\u00ecnh m\u1edbi ti\u1ebfp t\u1ee5c v\u1edbi d\u00f2ng <\/span><span style=\"font-weight: 400;\">print(&#8216;K\u1ebft th\u00fac&#8217;);<\/span><span style=\"font-weight: 400;\">. B\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y r\u00f5 s\u1ef1 tu\u1ea7n t\u1ef1 v\u00e0 vi\u1ec7c &#8220;ch\u1eb7n&#8221; lu\u1ed3ng th\u1ef1c thi.<\/span><\/p>\n<h3><b>\u01afu \u0111i\u1ec3m c\u1ee7a l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 hi\u1ec3u:<\/b><span style=\"font-weight: 400;\"> Lu\u1ed3ng th\u1ef1c thi tu\u1ea7n t\u1ef1 r\u1ea5t d\u1ec5 theo d\u00f5i v\u00e0 d\u1ef1 \u0111o\u00e1n. Ng\u01b0\u1eddi l\u1eadp tr\u00ecnh d\u1ec5 d\u00e0ng h\u00ecnh dung \u0111\u01b0\u1ee3c c\u00e1c b\u01b0\u1edbc m\u00e0 ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd th\u1ef1c hi\u1ec7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>D\u1ec5 d\u00e0ng theo d\u00f5i lu\u1ed3ng th\u1ef1c thi:<\/b><span style=\"font-weight: 400;\"> V\u1edbi vi\u1ec7c c\u00e1c t\u00e1c v\u1ee5 di\u1ec5n ra l\u1ea7n l\u01b0\u1ee3t, vi\u1ec7c g\u1ee1 l\u1ed7i v\u00e0 theo d\u00f5i lu\u1ed3ng \u0111i c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh tr\u1edf n\u00ean \u0111\u01a1n gi\u1ea3n h\u01a1n nhi\u1ec1u.<\/span><\/li>\n<\/ul>\n<h3><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00f3 th\u1ec3 g\u00e2y ra t\u00ecnh tr\u1ea1ng &#8220;\u0111\u1ee9ng&#8221; \u1ee9ng d\u1ee5ng (UI freezes):<\/b><span style=\"font-weight: 400;\"> \u0110\u00e2y l\u00e0 nh\u01b0\u1ee3c \u0111i\u1ec3m l\u1edbn nh\u1ea5t c\u1ee7a l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9, \u0111\u1eb7c bi\u1ec7t trong c\u00e1c \u1ee9ng d\u1ee5ng c\u00f3 giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng (UI) nh\u01b0 \u1ee9ng d\u1ee5ng di \u0111\u1ed9ng ho\u1eb7c web. N\u1ebfu m\u1ed9t t\u00e1c v\u1ee5 t\u1ed1n nhi\u1ec1u th\u1eddi gian (v\u00ed d\u1ee5: \u0111\u1ecdc m\u1ed9t file l\u1edbn t\u1eeb \u1ed5 c\u1ee9ng, th\u1ef1c hi\u1ec7n m\u1ed9t cu\u1ed9c g\u1ecdi API \u0111\u1ebfn server), to\u00e0n b\u1ed9 \u1ee9ng d\u1ee5ng c\u00f3 th\u1ec3 b\u1ecb &#8220;\u0111\u1ee9ng&#8221; v\u00e0 kh\u00f4ng ph\u1ea3n h\u1ed3i cho \u0111\u1ebfn khi t\u00e1c v\u1ee5 \u0111\u00f3 ho\u00e0n th\u00e0nh. \u0110i\u1ec1u n\u00e0y mang l\u1ea1i tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng r\u1ea5t t\u1ec7.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4ng hi\u1ec7u qu\u1ea3 cho c\u00e1c t\u00e1c v\u1ee5 li\u00ean quan \u0111\u1ebfn I\/O (Input\/Output) ho\u1eb7c c\u00e1c ho\u1ea1t \u0111\u1ed9ng c\u1ea7n ch\u1edd \u0111\u1ee3i:<\/b><span style=\"font-weight: 400;\"> C\u00e1c t\u00e1c v\u1ee5 nh\u01b0 \u0111\u1ecdc\/ghi file, t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u, ho\u1eb7c g\u1ecdi API th\u01b0\u1eddng m\u1ea5t m\u1ed9t kho\u1ea3ng th\u1eddi gian \u0111\u00e1ng k\u1ec3 \u0111\u1ec3 ho\u00e0n th\u00e0nh. Trong m\u00f4 h\u00ecnh \u0111\u1ed3ng b\u1ed9, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd ph\u1ea3i \u0111\u1ee3i m\u1ed9t c\u00e1ch l\u00e3ng ph\u00ed trong th\u1eddi gian n\u00e0y, thay v\u00ec c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c c\u00f4ng vi\u1ec7c kh\u00e1c.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Lap_trinh_asynchronous_trong_Dart\"><\/span><b>L\u1eadp tr\u00ecnh asynchronous trong Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u00ecm hi\u1ec3u v\u1ec1 l\u1eadp tr\u00ecnh asynchronous (hay l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9), ta c\u00f3 th\u1ec3 li\u00ean t\u01b0\u1edfng \u0111\u1ebfn m\u1ed9t v\u00ed d\u1ee5 th\u1ef1c t\u1ebf trong cu\u1ed9c s\u1ed1ng th\u01b0\u1eddng ng\u00e0y nh\u01b0 sau: B\u1ea1n \u0111ang g\u1ecdi m\u00f3n t\u1ea1i m\u1ed9t nh\u00e0 h\u00e0ng, thay v\u00ec \u0111\u1ee9ng \u0111\u1ee3i m\u00f3n \u0103n c\u1ee7a m\u00ecnh \u0111\u01b0\u1ee3c chu\u1ea9n b\u1ecb xong r\u1ed3i m\u1edbi c\u00f3 th\u1ec3 g\u1ecdi th\u00eam ho\u1eb7c l\u00e0m vi\u1ec7c kh\u00e1c, b\u1ea1n g\u1ecdi m\u00f3n xong v\u00e0 c\u00f3 th\u1ec3 tho\u1ea3i m\u00e1i tr\u00f2 chuy\u1ec7n v\u1edbi b\u1ea1n b\u00e8 ho\u1eb7c l\u01b0\u1edbt \u0111i\u1ec7n tho\u1ea1i. Khi m\u00f3n \u0103n s\u1eb5n s\u00e0ng, nh\u00e2n vi\u00ean s\u1ebd mang \u0111\u1ebfn cho b\u1ea1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u00f3 ch\u00ednh l\u00e0 tinh th\u1ea7n c\u1ee7a l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9: ch\u01b0\u01a1ng tr\u00ecnh c\u1ee7a b\u1ea1n c\u00f3 th\u1ec3 b\u1eaft \u0111\u1ea7u m\u1ed9t t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian (v\u00ed d\u1ee5: t\u1ea3i d\u1eef li\u1ec7u t\u1eeb internet, \u0111\u1ecdc file) v\u00e0 ti\u1ebfp t\u1ee5c th\u1ef1c hi\u1ec7n c\u00e1c c\u00f4ng vi\u1ec7c kh\u00e1c m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ch\u1edd \u0111\u1ee3i t\u00e1c v\u1ee5 \u0111\u00f3 ho\u00e0n th\u00e0nh. Khi t\u00e1c v\u1ee5 ho\u00e0n t\u1ea5t, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd \u0111\u01b0\u1ee3c th\u00f4ng b\u00e1o v\u00e0 x\u1eed l\u00fd k\u1ebft qu\u1ea3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 hi\u1ec7n th\u1ef1c h\u00f3a \u0111i\u1ec1u n\u00e0y trong Dart, ch\u00fang ta c\u00f3 ba kh\u00e1i ni\u1ec7m then ch\u1ed1t:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>async<\/b><b> v\u00e0 <\/b><b>await<\/b><b> keywords:<\/b><span style=\"font-weight: 400;\"> \u0110\u00e2y l\u00e0 b\u1ed9 \u0111\u00f4i m\u1ea1nh m\u1ebd gi\u00fap b\u1ea1n vi\u1ebft code b\u1ea5t \u0111\u1ed3ng b\u1ed9 m\u1ed9t c\u00e1ch d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 qu\u1ea3n l\u00fd, g\u1ea7n gi\u1ed1ng nh\u01b0 code \u0111\u1ed3ng b\u1ed9. Khi b\u1ea1n \u0111\u00e1nh d\u1ea5u m\u1ed9t h\u00e0m l\u00e0 <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> b\u00ean trong h\u00e0m \u0111\u00f3 \u0111\u1ec3 t\u1ea1m d\u1eebng vi\u1ec7c th\u1ef1c thi cho \u0111\u1ebfn khi m\u1ed9t <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> ho\u00e0n th\u00e0nh, m\u00e0 kh\u00f4ng l\u00e0m &#8220;\u0111\u00f3ng b\u0103ng&#8221; to\u00e0n b\u1ed9 \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Future<\/b><b> objects:<\/b><span style=\"font-weight: 400;\"> M\u1ed9t <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho k\u1ebft qu\u1ea3 c\u1ee7a m\u1ed9t ph\u00e9p t\u00ednh b\u1ea5t \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 s\u1ebd ho\u00e0n th\u00e0nh trong t\u01b0\u01a1ng lai. N\u00f3 gi\u1ed1ng nh\u01b0 m\u1ed9t l\u1eddi h\u1ee9a: ho\u1eb7c l\u00e0 t\u00e1c v\u1ee5 s\u1ebd th\u00e0nh c\u00f4ng v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb, ho\u1eb7c l\u00e0 n\u00f3 s\u1ebd th\u1ea5t b\u1ea1i v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t l\u1ed7i.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">Future&lt;String&gt; fetchData() async {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ Gi\u1ea3 s\u1eed \u0111\u00e2y l\u00e0 m\u1ed9t t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian nh\u01b0 g\u1ecdi API<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0await Future.delayed(Duration(seconds: 2));<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0return \"D\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea3i!\";<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">void main() async {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print(\"B\u1eaft \u0111\u1ea7u t\u1ea3i d\u1eef li\u1ec7u...\");<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0String data = await fetchData();<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print(data); \/\/ S\u1ebd in ra \"D\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea3i!\" sau 2 gi\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print(\"C\u00f4ng vi\u1ec7c kh\u00e1c v\u1eabn ti\u1ebfp t\u1ee5c...\");<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 tr\u00ean, h\u00e0m <\/span><span style=\"font-weight: 400;\">fetchData<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">, v\u00e0 ch\u00fang ta s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ee3i <\/span><span style=\"font-weight: 400;\">Future.delayed<\/span><span style=\"font-weight: 400;\"> ho\u00e0n th\u00e0nh (m\u00f4 ph\u1ecfng m\u1ed9t t\u00e1c v\u1ee5 m\u1ea5t th\u1eddi gian). Sau \u0111\u00f3, ch\u00fang ta <\/span><span style=\"font-weight: 400;\">await fetchData()<\/span><span style=\"font-weight: 400;\"> trong <\/span><span style=\"font-weight: 400;\">main<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ee3i d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 tr\u01b0\u1edbc khi in ra.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Stream<\/b><b> objects:<\/b><span style=\"font-weight: 400;\"> N\u1ebfu <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho m\u1ed9t k\u1ebft qu\u1ea3 duy nh\u1ea5t s\u1ebd \u0111\u1ebfn trong t\u01b0\u01a1ng lai, th\u00ec <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho m\u1ed9t chu\u1ed7i c\u00e1c s\u1ef1 ki\u1ec7n ho\u1eb7c d\u1eef li\u1ec7u b\u1ea5t \u0111\u1ed3ng b\u1ed9 theo th\u1eddi gian. H\u00e3y t\u01b0\u1edfng t\u01b0\u1ee3ng nh\u01b0 b\u1ea1n \u0111ang xem m\u1ed9t k\u00eanh truy\u1ec1n h\u00ecnh tr\u1ef1c ti\u1ebfp: b\u1ea1n kh\u00f4ng ch\u1ec9 nh\u1eadn \u0111\u01b0\u1ee3c m\u1ed9t th\u00f4ng tin m\u00e0 l\u00e0 m\u1ed9t lu\u1ed3ng th\u00f4ng tin li\u00ean t\u1ee5c. <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> r\u1ea5t h\u1eefu \u00edch cho c\u00e1c t\u00e1c v\u1ee5 nh\u01b0 l\u1eafng nghe s\u1ef1 ki\u1ec7n t\u1eeb ng\u01b0\u1eddi d\u00f9ng, \u0111\u1ecdc d\u1eef li\u1ec7u t\u1eeb file theo t\u1eebng ph\u1ea7n, ho\u1eb7c nh\u1eadn th\u00f4ng tin c\u1eadp nh\u1eadt li\u00ean t\u1ee5c t\u1eeb server.<\/span><\/li>\n<\/ul>\n<h3><b>\u01afu \u0111i\u1ec3m c\u1ee7a l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3 kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i c\u1ee7a \u1ee9ng d\u1ee5ng:<\/b><span style=\"font-weight: 400;\"> \u1ee8ng d\u1ee5ng c\u1ee7a b\u1ea1n s\u1ebd kh\u00f4ng b\u1ecb &#8220;\u0111\u1ee9ng&#8221; khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian, mang l\u1ea1i tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng m\u01b0\u1ee3t m\u00e0 h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t cho c\u00e1c t\u00e1c v\u1ee5 li\u00ean quan \u0111\u1ebfn I\/O, network, ho\u1eb7c c\u00e1c ho\u1ea1t \u0111\u1ed9ng t\u1ed1n th\u1eddi gian:<\/b><span style=\"font-weight: 400;\"> Thay v\u00ec ch\u1edd \u0111\u1ee3i m\u1ed9t c\u00e1ch th\u1ee5 \u0111\u1ed9ng, ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 ti\u1ebfp t\u1ee5c th\u1ef1c hi\u1ec7n c\u00e1c c\u00f4ng vi\u1ec7c kh\u00e1c trong khi ch\u1edd \u0111\u1ee3i d\u1eef li\u1ec7u t\u1eeb m\u1ea1ng ho\u1eb7c file.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 \u0111\u1ed3ng th\u1eddi (concurrency):<\/b><span style=\"font-weight: 400;\"> M\u1eb7c d\u00f9 Dart s\u1eed d\u1ee5ng m\u1ed9t single thread v\u00e0 event loop, l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 cho ph\u00e9p b\u1ea1n m\u00f4 ph\u1ecfng vi\u1ec7c th\u1ef1c hi\u1ec7n nhi\u1ec1u t\u00e1c v\u1ee5 c\u00f9ng m\u1ed9t l\u00fac, gi\u00fap t\u1eadn d\u1ee5ng t\u1ed1i \u0111a t\u00e0i nguy\u00ean h\u1ec7 th\u1ed1ng.<\/span><\/li>\n<\/ul>\n<h3><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00f3 th\u1ec3 ph\u1ee9c t\u1ea1p h\u01a1n \u0111\u1ec3 vi\u1ebft v\u00e0 hi\u1ec3u so v\u1edbi code \u0111\u1ed3ng b\u1ed9:<\/b><span style=\"font-weight: 400;\"> Vi\u1ec7c qu\u1ea3n l\u00fd lu\u1ed3ng th\u1ef1c thi v\u00e0 x\u1eed l\u00fd c\u00e1c tr\u1ea1ng th\u00e1i kh\u00e1c nhau c\u1ee7a <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 \u0111\u00f2i h\u1ecfi t\u01b0 duy kh\u00e1c bi\u1ec7t so v\u1edbi l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Y\u00eau c\u1ea7u qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i v\u00e0 x\u1eed l\u00fd l\u1ed7i c\u1ea9n th\u1eadn h\u01a1n:<\/b><span style=\"font-weight: 400;\"> B\u1ea1n c\u1ea7n \u0111\u1ea3m b\u1ea3o r\u1eb1ng \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh c\u00f3 th\u1ec3 x\u1eed l\u00fd c\u00e1c tr\u01b0\u1eddng h\u1ee3p l\u1ed7i x\u1ea3y ra trong qu\u00e1 tr\u00ecnh th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 m\u1ed9t c\u00e1ch ch\u00ednh x\u00e1c.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"So_sanh_lap_trinh_synchronous_va_asynchronous_trong_Dart\"><\/span><b>So s\u00e1nh l\u1eadp tr\u00ecnh synchronous v\u00e0 asynchronous trong Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Khi \u0111i s\u00e2u t\u00ecm hi\u1ec3u v\u1ec1 hai k\u1ef9 thu\u1eadt l\u1eadp tr\u00ecnh n\u00e0y ta c\u00f3 th\u1ec3 c\u00f3 r\u00fat ra \u0111\u01b0\u1ee3c m\u1ed9t v\u00e0i c\u00e1c s\u1ef1 kh\u00e1c bi\u1ec7t nh\u01b0 sau:<\/span><\/p>\n<h3><b>\u0110\u1ed9 ph\u1ee9c t\u1ea1p trong code<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Code th\u01b0\u1eddng ch\u1ea1y tu\u1ea7n t\u1ef1 t\u1eeb tr\u00ean xu\u1ed1ng d\u01b0\u1edbi. Lu\u1ed3ng th\u1ef1c thi r\u1ea5t d\u1ec5 theo d\u00f5i. M\u1ed7i l\u1ec7nh ho\u00e0n th\u00e0nh tr\u01b0\u1edbc khi l\u1ec7nh ti\u1ebfp theo b\u1eaft \u0111\u1ea7u. Logic \u0111\u01a1n gi\u1ea3n, kh\u00f4ng c\u1ea7n c\u00e1c c\u1ea5u tr\u00fac \u0111\u1eb7c bi\u1ec7t \u0111\u1ec3 qu\u1ea3n l\u00fd ch\u1edd \u0111\u1ee3i.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Code kh\u00f4ng ch\u1ea1y ho\u00e0n to\u00e0n tu\u1ea7n t\u1ef1. C\u1ea7n s\u1eed d\u1ee5ng c\u00e1c c\u01a1 ch\u1ebf nh\u01b0 Future (ho\u1eb7c Promise trong c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c), Stream, callbacks, ho\u1eb7c c\u1eb7p t\u1eeb kh\u00f3a async\/await. Vi\u1ec7c qu\u1ea3n l\u00fd lu\u1ed3ng th\u1ef1c thi, th\u1ee9 t\u1ef1 ho\u00e0n th\u00e0nh t\u00e1c v\u1ee5, v\u00e0 gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p h\u01a1n.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Callback c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn &#8220;callback hell&#8221; (nhi\u1ec1u callback l\u1ed3ng nhau kh\u00f3 \u0111\u1ecdc). async\/await gi\u00fap gi\u1ea3m \u0111\u00e1ng k\u1ec3 \u0111\u1ed9 ph\u1ee9c t\u1ea1p v\u1ec1 m\u1eb7t c\u00fa ph\u00e1p, l\u00e0m code tr\u00f4ng gi\u1ed1ng \u0111\u1ed3ng b\u1ed9 h\u01a1n, nh\u01b0ng v\u1eabn c\u1ea7n hi\u1ec3u b\u1ea3n ch\u1ea5t kh\u00f4ng ch\u1eb7n (non-blocking) b\u00ean d\u01b0\u1edbi.<\/span><\/p>\n<h3><b>Kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i c\u1ee7a \u1ee9ng d\u1ee5ng<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">N\u1ebfu m\u1ed9t t\u00e1c v\u1ee5 \u0111\u1ed3ng b\u1ed9 m\u1ea5t nhi\u1ec1u th\u1eddi gian (v\u00ed d\u1ee5: g\u1ecdi m\u1ea1ng, \u0111\u1ecdc file l\u1edbn) \u0111\u01b0\u1ee3c th\u1ef1c thi tr\u00ean lu\u1ed3ng ch\u00ednh (\u0111\u1eb7c bi\u1ec7t l\u00e0 UI thread), to\u00e0n b\u1ed9 \u1ee9ng d\u1ee5ng s\u1ebd b\u1ecb ch\u1eb7n (block) ho\u1eb7c &#8220;\u0111\u01a1&#8221;. Ng\u01b0\u1eddi d\u00f9ng kh\u00f4ng th\u1ec3 t\u01b0\u01a1ng t\u00e1c v\u1edbi giao di\u1ec7n (nh\u1ea5n n\u00fat, cu\u1ed9n&#8230;) cho \u0111\u1ebfn khi t\u00e1c v\u1ee5 \u0111\u00f3 ho\u00e0n th\u00e0nh.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Cho ph\u00e9p c\u00e1c t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian ch\u1ea1y \u1edf &#8220;h\u1eadu tr\u01b0\u1eddng&#8221; m\u00e0 kh\u00f4ng ch\u1eb7n lu\u1ed3ng ch\u00ednh. Khi b\u1ea1n await m\u1ed9t Future, ch\u1ec9 c\u00f3 h\u00e0m async hi\u1ec7n t\u1ea1i t\u1ea1m d\u1eebng, c\u00f2n event loop v\u1eabn ti\u1ebfp t\u1ee5c x\u1eed l\u00fd c\u00e1c s\u1ef1 ki\u1ec7n kh\u00e1c (nh\u01b0 input ng\u01b0\u1eddi d\u00f9ng, v\u1ebd l\u1ea1i UI).<\/span><\/p>\n<h3><b>Hi\u1ec7u su\u1ea5t cho c\u00e1c t\u00e1c v\u1ee5 kh\u00e1c nhau<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi c\u00e1c t\u00e1c v\u1ee5 I\/O-bound (ph\u1ee5 thu\u1ed9c v\u00e0o Input\/Output nh\u01b0 m\u1ea1ng, \u0111\u0129a c\u1ee9ng, database), m\u00f4 h\u00ecnh \u0111\u1ed3ng b\u1ed9 r\u1ea5t kh\u00f4ng hi\u1ec7u qu\u1ea3. Lu\u1ed3ng th\u1ef1c thi ph\u1ea3i ch\u1edd \u0111\u1ee3i I\/O ho\u00e0n th\u00e0nh m\u1ed9t c\u00e1ch b\u1ecb \u0111\u1ed9ng, l\u00e3ng ph\u00ed t\u00e0i nguy\u00ean CPU trong th\u1eddi gian ch\u1edd \u0111\u00f3. \u0110\u1ed1i v\u1edbi c\u00e1c t\u00e1c v\u1ee5 CPU-bound (t\u00ednh to\u00e1n n\u1eb7ng), vi\u1ec7c ch\u1ea1y \u0111\u1ed3ng b\u1ed9 tr\u00ean lu\u1ed3ng ch\u00ednh c\u0169ng s\u1ebd ch\u1eb7n \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u1ef1c k\u1ef3 hi\u1ec7u qu\u1ea3 cho c\u00e1c t\u00e1c v\u1ee5 I\/O-bound. Trong khi ch\u1edd \u0111\u1ee3i I\/O (v\u00ed d\u1ee5: ch\u1edd ph\u1ea3n h\u1ed3i t\u1eeb server), lu\u1ed3ng th\u1ef1c thi c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c gi\u1ea3i ph\u00f3ng \u0111\u1ec3 l\u00e0m vi\u1ec7c kh\u00e1c (x\u1eed l\u00fd s\u1ef1 ki\u1ec7n UI, ch\u1ea1y c\u00e1c t\u00e1c v\u1ee5 async kh\u00e1c). \u0110i\u1ec1u n\u00e0y t\u0103ng th\u00f4ng l\u01b0\u1ee3ng (throughput) v\u00e0 kh\u1ea3 n\u0103ng s\u1eed d\u1ee5ng t\u00e0i nguy\u00ean c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi c\u00e1c t\u00e1c v\u1ee5 CPU-bound n\u1eb7ng, b\u1ea3n th\u00e2n async\/await tr\u00ean m\u1ed9t lu\u1ed3ng \u0111\u01a1n (nh\u01b0 m\u1eb7c \u0111\u1ecbnh trong Dart\/JS) kh\u00f4ng l\u00e0m t\u0103ng t\u1ed1c \u0111\u1ed9 t\u00ednh to\u00e1n \u0111\u00f3 (v\u00ec n\u00f3 v\u1eabn ch\u1ea1y tr\u00ean c\u00f9ng lu\u1ed3ng). Tuy nhi\u00ean, n\u00f3 v\u1eabn gi\u00fap \u1ee9ng d\u1ee5ng kh\u00f4ng b\u1ecb \u0111\u01a1 trong khi t\u00ednh to\u00e1n \u0111\u00f3 di\u1ec5n ra (n\u1ebfu t\u00ednh to\u00e1n \u0111\u00f3 \u0111\u01b0\u1ee3c await).\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 th\u1ef1c s\u1ef1 ch\u1ea1y song song c\u00e1c t\u00e1c v\u1ee5 CPU-bound, c\u1ea7n d\u00f9ng c\u01a1 ch\u1ebf kh\u00e1c nh\u01b0 Isolate trong Dart (ho\u1eb7c Web Workers trong JS), v\u00e0 vi\u1ec7c giao ti\u1ebfp v\u1edbi c\u00e1c Isolate\/Worker n\u00e0y th\u01b0\u1eddng l\u1ea1i d\u00f9ng m\u00f4 h\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/p>\n<h3><b>Qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Tr\u1ea1ng th\u00e1i \u1ee9ng d\u1ee5ng thay \u0111\u1ed5i m\u1ed9t c\u00e1ch tu\u1ea7n t\u1ef1 v\u00e0 d\u1ef1 \u0111o\u00e1n \u0111\u01b0\u1ee3c. Khi m\u1ed9t h\u00e0m ch\u1ea1y xong, b\u1ea1n bi\u1ebft ch\u1eafc ch\u1eafn tr\u1ea1ng th\u00e1i \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt (ho\u1eb7c ch\u01b0a). D\u1ec5 d\u00e0ng suy lu\u1eadn v\u1ec1 tr\u1ea1ng th\u00e1i t\u1ea1i b\u1ea5t k\u1ef3 \u0111i\u1ec3m n\u00e0o trong code.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p h\u01a1n. Tr\u1ea1ng th\u00e1i c\u00f3 th\u1ec3 b\u1ecb thay \u0111\u1ed5i b\u1edfi nhi\u1ec1u t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 ho\u00e0n th\u00e0nh v\u00e0o nh\u1eefng th\u1eddi \u0111i\u1ec3m kh\u00e1c nhau. C\u1ea7n c\u1ea9n th\u1eadn v\u1edbi &#8220;race conditions&#8221; (khi nhi\u1ec1u t\u00e1c v\u1ee5 c\u1ed1 g\u1eafng \u0111\u1ecdc\/ghi c\u00f9ng m\u1ed9t tr\u1ea1ng th\u00e1i v\u00e0 k\u1ebft qu\u1ea3 cu\u1ed1i c\u00f9ng ph\u1ee5 thu\u1ed9c v\u00e0o th\u1ee9 t\u1ef1 ho\u00e0n th\u00e0nh kh\u00f4ng \u0111o\u00e1n tr\u01b0\u1edbc \u0111\u01b0\u1ee3c). C\u1ea7n \u0111\u1ea3m b\u1ea3o UI \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt ch\u00ednh x\u00e1c khi d\u1eef li\u1ec7u \u0111\u1ebfn b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/p>\n<h3><b>X\u1eed l\u00fd l\u1ed7i<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Th\u01b0\u1eddng s\u1eed d\u1ee5ng kh\u1ed1i <\/span><span style=\"font-weight: 400;\">try&#8230;catch<\/span><span style=\"font-weight: 400;\"> ti\u00eau chu\u1ea9n. L\u1ed7i \u0111\u01b0\u1ee3c n\u00e9m (throw) v\u00e0 b\u1eaft (catch) m\u1ed9t c\u00e1ch tu\u1ea7n t\u1ef1 theo ng\u0103n x\u1ebfp cu\u1ed9c g\u1ecdi (call stack). Lu\u1ed3ng x\u1eed l\u00fd l\u1ed7i kh\u00e1 r\u00f5 r\u00e0ng.<\/span><\/p>\n<ul>\n<li aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">L\u1ed7i x\u1ea3y ra trong m\u1ed9t ho\u1ea1t \u0111\u1ed9ng b\u1ea5t \u0111\u1ed3ng b\u1ed9 th\u01b0\u1eddng \u0111\u01b0\u1ee3c g\u00f3i g\u1ecdn trong \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> (ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\">).<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">V\u1edbi <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\">, b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng <\/span><span style=\"font-weight: 400;\">try&#8230;catch<\/span><span style=\"font-weight: 400;\"> bao quanh bi\u1ec3u th\u1ee9c <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\">, l\u00e0m cho vi\u1ec7c x\u1eed l\u00fd l\u1ed7i tr\u00f4ng r\u1ea5t gi\u1ed1ng code \u0111\u1ed3ng b\u1ed9.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng d\u00f9ng <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\">, b\u1ea1n ph\u1ea3i d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">.catchError()<\/span><span style=\"font-weight: 400;\"> c\u1ee7a <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c l\u1eafng nghe s\u1ef1 ki\u1ec7n l\u1ed7i tr\u00ean <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u00e1c l\u1ed7i kh\u00f4ng \u0111\u01b0\u1ee3c x\u1eed l\u00fd (unhandled exceptions) trong code b\u1ea5t \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 kh\u00f3 theo d\u00f5i h\u01a1n v\u00e0 \u0111\u00f4i khi c\u00f3 th\u1ec3 l\u00e0m s\u1eadp \u1ee9ng d\u1ee5ng ho\u1eb7c b\u1ecb &#8220;nu\u1ed1t&#8221; m\u1ea5t n\u1ebfu kh\u00f4ng c\u1ea9n th\u1eadn (v\u00ed d\u1ee5: g\u1ecdi m\u1ed9t h\u00e0m tr\u1ea3 v\u1ec1 <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> nh\u01b0ng kh\u00f4ng <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> hay <\/span><span style=\"font-weight: 400;\">.then()<\/span><span style=\"font-weight: 400;\"> n\u00f3).<\/span><\/li>\n<\/ul>\n<h3><b>Kh\u1ea3 n\u0103ng \u0111\u1ecdc hi\u1ec3u v\u00e0 b\u1ea3o tr\u00ec code<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>\u0110\u1ed3ng b\u1ed9 (Synchronous):<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Code r\u1ea5t d\u1ec5 \u0111\u1ecdc v\u00e0 theo d\u00f5i lu\u1ed3ng logic tu\u1ea7n t\u1ef1. D\u1ec5 d\u00e0ng b\u1ea3o tr\u00ec khi logic \u0111\u01a1n gi\u1ea3n.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><strong>B\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous):<\/strong><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng Callbacks: C\u00f3 th\u1ec3 tr\u1edf n\u00ean r\u1ea5t kh\u00f3 \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec, d\u1ec5 r\u01a1i v\u00e0o &#8220;callback hell&#8221;.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng Future.then(): D\u1ec5 \u0111\u1ecdc h\u01a1n callback, nh\u01b0ng chu\u1ed7i .then() d\u00e0i c\u0169ng c\u00f3 th\u1ec3 l\u00e0m code kh\u00f3 theo d\u00f5i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng async\/await: C\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3 kh\u1ea3 n\u0103ng \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec. Code tr\u00f4ng g\u1ea7n gi\u1ed1ng nh\u01b0 code \u0111\u1ed3ng b\u1ed9, gi\u00fap lu\u1ed3ng logic d\u1ec5 hi\u1ec3u h\u01a1n r\u1ea5t nhi\u1ec1u. Tuy nhi\u00ean, ng\u01b0\u1eddi \u0111\u1ecdc v\u1eabn c\u1ea7n hi\u1ec3u r\u1eb1ng await ch\u1ec9 t\u1ea1m d\u1eebng h\u00e0m c\u1ee5c b\u1ed9, kh\u00f4ng ph\u1ea3i to\u00e0n b\u1ed9 \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 c\u00f3 c\u00e1i nh\u00ecn tr\u1ef1c quan m\u00f4 t\u1ea3 hai k\u1ef9 thu\u1eadt l\u1eadp tr\u00ecnh n\u00e0y trong code, m\u00ecnh c\u00f3 setup m\u1ed9t \u0111o\u1ea1n code \u0111\u01a1n gi\u1ea3n nh\u01b0 sau:<\/span><\/p>\n<h3><b>Code \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">import 'dart:io'; \/\/ C\u1ea7n th\u01b0 vi\u1ec7n dart:io \u0111\u1ec3 d\u00f9ng sleep<\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">\/\/ H\u00e0m \u0111\u1ed3ng b\u1ed9 gi\u1ea3 l\u1eadp t\u00e1c v\u1ee5 n\u1eb7ng ho\u1eb7c ch\u1edd \u0111\u1ee3i I\/O (nh\u01b0ng d\u00f9ng sleep \u0111\u1ec3 ch\u1eb7n)<\/span>\r\n<span style=\"font-weight: 400;\">String processTaskSync(String taskName, int durationSeconds) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('[$taskName] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (\u0111\u1ed3ng b\u1ed9)...');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0try {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ sleep() s\u1ebd ch\u1eb7n ho\u00e0n to\u00e0n lu\u1ed3ng hi\u1ec7n t\u1ea1i trong N gi\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0sleep(Duration(seconds: durationSeconds));<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('[$taskName] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return '$taskName: OK';<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0} catch (e) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('[$taskName] G\u1eb7p l\u1ed7i: $e');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return '$taskName: Failed';<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">void main() {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('B\u1eaft \u0111\u1ea7u ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task A.');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0String resultA = processTaskSync('Task A', 2); \/\/ Ch\u1ea1y Task A, ch\u01b0\u01a1ng tr\u00ecnh d\u1eebng \u1edf \u0111\u00e2y 2 gi\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('K\u1ebft qu\u1ea3 Task A: $resultA');<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('\u0110\u00e3 xong Task A.');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('--------------------');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task B.');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ M\u1ecdi th\u1ee9 d\u1eebng l\u1ea1i ch\u1edd Task B ho\u00e0n th\u00e0nh, k\u1ec3 c\u1ea3 vi\u1ec7c in \"\u0110\u00e3 xong Task B.\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0String resultB = processTaskSync('Task B', 1); \/\/ Ch\u1ea1y Task B, ch\u01b0\u01a1ng tr\u00ecnh d\u1eebng \u1edf \u0111\u00e2y 1 gi\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('K\u1ebft qu\u1ea3 Task B: $resultB');<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('\u0110\u00e3 xong Task B.');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('--------------------');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('K\u1ebft th\u00fac ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.'); \/\/ D\u00f2ng n\u00e0y ch\u1ec9 ch\u1ea1y sau khi t\u1ea5t c\u1ea3 c\u00e1c task \u0111\u1ed3ng b\u1ed9 ho\u00e0n th\u00e0nh.<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/*<\/span>\r\n<span style=\"font-weight: 400;\">Output s\u1ebd tu\u1ea7n t\u1ef1 v\u00e0 c\u00f3 \u0111\u1ed9 tr\u1ec5 r\u00f5 r\u00e0ng:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task A.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task A] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (\u0111\u1ed3ng b\u1ed9)...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">(ch\u1edd 2 gi\u00e2y)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task A] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 Task A: Task A: OK<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u0110\u00e3 xong Task A.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">--------------------<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task B.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task B] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (\u0111\u1ed3ng b\u1ed9)...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">(ch\u1edd 1 gi\u00e2y)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task B] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 Task B: Task B: OK<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u0110\u00e3 xong Task B.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">--------------------<\/span>\r\n\r\n<span style=\"font-weight: 400;\">K\u1ebft th\u00fac ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">*\/<\/span><\/pre>\n<h3><b>Code b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u1edbi <\/b><b>async<\/b><b>\/<\/b><b>await<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">import 'dart:async';<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ H\u00e0m b\u1ea5t \u0111\u1ed3ng b\u1ed9 gi\u1ea3 l\u1eadp t\u00e1c v\u1ee5 ch\u1edd I\/O (kh\u00f4ng ch\u1eb7n lu\u1ed3ng)<\/span>\r\n<span style=\"font-weight: 400;\">\/\/ Tr\u1ea3 v\u1ec1 Future&lt;String&gt; v\u00e0 \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 async<\/span>\r\n<span style=\"font-weight: 400;\">Future&lt;String&gt; processTaskAsync(String taskName, int durationSeconds, {bool makeError = false}) async {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('[$taskName] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (b\u1ea5t \u0111\u1ed3ng b\u1ed9)...');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0try {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ await Future.delayed ch\u1ec9 t\u1ea1m d\u1eebng h\u00e0m n\u00e0y, kh\u00f4ng ch\u1eb7n main thread<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0await Future.delayed(Duration(seconds: durationSeconds));<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0if (makeError) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0throw Exception('L\u1ed7i c\u1ed1 \u00fd t\u1eeb $taskName');<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('[$taskName] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return '$taskName: OK';<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0} catch (e) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ L\u1ed7i \u0111\u01b0\u1ee3c b\u1eaft trong h\u00e0m async ho\u1eb7c n\u01a1i g\u1ecdi h\u00e0m n\u00e0y d\u00f9ng await trong try-catch<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('[$taskName] G\u1eb7p l\u1ed7i b\u00ean trong h\u00e0m: $e');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ N\u00e9m l\u1ea1i l\u1ed7i \u0111\u1ec3 n\u01a1i g\u1ecdi (main) c\u00f3 th\u1ec3 b\u1eaft \u0111\u01b0\u1ee3c n\u1ebfu mu\u1ed1n<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0throw e;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ Ho\u1eb7c return gi\u00e1 tr\u1ecb b\u00e1o l\u1ed7i: return '$taskName: Failed - $e';<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ main ph\u1ea3i l\u00e0 async \u0111\u1ec3 d\u00f9ng await \u1edf c\u1ea5p cao nh\u1ea5t<\/span>\r\n<span style=\"font-weight: 400;\">Future&lt;void&gt; main() async {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('B\u1eaft \u0111\u1ea7u ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task A.');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ G\u1ecdi Task A v\u00e0 ch\u1edd (await) n\u00f3 ho\u00e0n th\u00e0nh<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0try {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ Ch\u01b0\u01a1ng tr\u00ecnh t\u1ea1m d\u1eebng *t\u1ea1i d\u00f2ng await n\u00e0y* trong h\u00e0m main,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ nh\u01b0ng event loop v\u1eabn ch\u1ea1y \u0111\u1ec3 x\u1eed l\u00fd vi\u1ec7c kh\u00e1c (n\u1ebfu c\u00f3)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0String resultA = await processTaskAsync('Task A', 2);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('K\u1ebft qu\u1ea3 Task A: $resultA');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0} catch (e) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('B\u1eaft \u0111\u01b0\u1ee3c l\u1ed7i Task A t\u1eeb main: $e');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('\u0110\u00e3 xong Task A (ho\u1eb7c x\u1eed l\u00fd l\u1ed7i xong).'); \/\/ Ch\u1ea1y ngay sau khi await Task A ho\u00e0n th\u00e0nh ho\u1eb7c b\u1ecb l\u1ed7i<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('--------------------');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('*** Trong l\u00fac Task A \u0111ang ch\u1ea1y (n\u1ebfu kh\u00f4ng await), ho\u1eb7c sau khi g\u1ecdi Task A, c\u00f3 th\u1ec3 l\u00e0m vi\u1ec7c kh\u00e1c \u1edf \u0111\u00e2y! ***');<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('--------------------');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task B (c\u00f3 l\u1ed7i) v\u00e0 Task C (kh\u00f4ng ch\u1edd).');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ G\u1ecdi Task B v\u00e0 ch\u1edd (await), c\u00f3 x\u1eed l\u00fd l\u1ed7i<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0try {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0String resultB = await processTaskAsync('Task B', 1, makeError: true);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('K\u1ebft qu\u1ea3 Task B: $resultB'); \/\/ S\u1ebd kh\u00f4ng ch\u1ea1y \u0111\u1ebfn \u0111\u00e2y v\u00ec c\u00f3 l\u1ed7i<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0} catch (e) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0print('B\u1eaft \u0111\u01b0\u1ee3c l\u1ed7i Task B t\u1eeb main: $e'); \/\/ L\u1ed7i s\u1ebd \u0111\u01b0\u1ee3c b\u1eaft \u1edf \u0111\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('\u0110\u00e3 xong Task B (ho\u1eb7c x\u1eed l\u00fd l\u1ed7i xong).');<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ G\u1ecdi Task C nh\u01b0ng kh\u00f4ng 'await' - n\u00f3 s\u1ebd ch\u1ea1y ng\u1ea7m<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0processTaskAsync('Task C (kh\u00f4ng await)', 3);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0print('\u0110\u00e3 g\u1ecdi Task C m\u00e0 kh\u00f4ng ch\u1edd n\u00f3 xong.'); \/\/ D\u00f2ng n\u00e0y in ra ngay l\u1eadp t\u1ee9c<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0print('--------------------');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0print('K\u1ebft th\u00fac ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh (h\u00e0m main k\u1ebft th\u00fac).');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\/\/ L\u01b0u \u00fd: Task C v\u1eabn c\u00f3 th\u1ec3 \u0111ang ch\u1ea1y ng\u1ea7m sau khi main k\u1ebft th\u00fac!<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/*<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Output c\u00f3 th\u1ec3 tr\u00f4ng nh\u01b0 th\u1ebf n\u00e0y (th\u1ee9 t\u1ef1 c\u00e1c d\u00f2ng cu\u1ed1i c\u1ee7a Task C c\u00f3 th\u1ec3 kh\u00e1c):<\/span>\r\n\r\n\r\n\r\n\r\n<span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task A.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task A] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (b\u1ea5t \u0111\u1ed3ng b\u1ed9)...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">(ch\u1edd 2 gi\u00e2y - nh\u01b0ng ch\u01b0\u01a1ng tr\u00ecnh kh\u00f4ng b\u1ecb ch\u1eb7n ho\u00e0n to\u00e0n \u1edf \u0111\u00e2y)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task A] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 Task A: Task A: OK<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u0110\u00e3 xong Task A (ho\u1eb7c x\u1eed l\u00fd l\u1ed7i xong).<\/span>\r\n\r\n<span style=\"font-weight: 400;\">--------------------<\/span>\r\n\r\n<span style=\"font-weight: 400;\">*** Trong l\u00fac Task A \u0111ang ch\u1ea1y (n\u1ebfu kh\u00f4ng await), ho\u1eb7c sau khi g\u1ecdi Task A, c\u00f3 th\u1ec3 l\u00e0m vi\u1ec7c kh\u00e1c \u1edf \u0111\u00e2y! ***<\/span>\r\n\r\n<span style=\"font-weight: 400;\">--------------------<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Chu\u1ea9n b\u1ecb th\u1ef1c hi\u1ec7n Task B (c\u00f3 l\u1ed7i) v\u00e0 Task C (kh\u00f4ng ch\u1edd).<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task B] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (b\u1ea5t \u0111\u1ed3ng b\u1ed9)...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">(ch\u1edd 1 gi\u00e2y)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task B] G\u1eb7p l\u1ed7i b\u00ean trong h\u00e0m: Exception: L\u1ed7i c\u1ed1 \u00fd t\u1eeb Task B<\/span>\r\n\r\n<span style=\"font-weight: 400;\">B\u1eaft \u0111\u01b0\u1ee3c l\u1ed7i Task B t\u1eeb main: Exception: L\u1ed7i c\u1ed1 \u00fd t\u1eeb Task B<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u0110\u00e3 xong Task B (ho\u1eb7c x\u1eed l\u00fd l\u1ed7i xong).<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task C (kh\u00f4ng await)] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd (b\u1ea5t \u0111\u1ed3ng b\u1ed9)...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u0110\u00e3 g\u1ecdi Task C m\u00e0 kh\u00f4ng ch\u1edd n\u00f3 xong.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">--------------------<\/span>\r\n\r\n<span style=\"font-weight: 400;\">K\u1ebft th\u00fac ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh (h\u00e0m main k\u1ebft th\u00fac).<\/span>\r\n\r\n<span style=\"font-weight: 400;\">(ch\u1edd th\u00eam ch\u00fat n\u1eefa cho Task C ho\u00e0n th\u00e0nh)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[Task C (kh\u00f4ng await)] X\u1eed l\u00fd ho\u00e0n th\u00e0nh.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">*\/<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Sau khi \u0111\u00e3 t\u00ecm hi\u1ec3u qua c\u00e1c \u0111\u1ecbnh ngh\u0129a v\u00e0 v\u00ed d\u1ee5 k\u1ec3 tr\u00ean c\u1ee7a c\u1ea3 hai ph\u01b0\u01a1ng ph\u00e1p tr\u00ean, ch\u00fang ta c\u00f3 th\u1ec3 t\u00f3m g\u1ecdn l\u1ea1i s\u1ef1 gi\u1ed1ng nhau v\u00e0 kh\u00e1c nhau gi\u1eefa hai ph\u01b0\u01a1ng ph\u00e1p tr\u00ean th\u00f4ng qua b\u1ea3ng t\u1ed5ng h\u1ee3p d\u01b0\u1edbi \u0111\u00e2y:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Ti\u00eau ch\u00ed so s\u00e1nh\u00a0<\/b><\/td>\n<td><b>L\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 (Synchronous)<\/b><\/td>\n<td><b>L\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 (Asynchronous)<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>\u0110\u1ed9 ph\u1ee9c t\u1ea1p c\u1ee7a code<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n, \u0111\u1eb7c bi\u1ec7t v\u1edbi c\u00e1c t\u00e1c v\u1ee5 tu\u1ea7n t\u1ef1.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ph\u1ee9c t\u1ea1p h\u01a1n ban \u0111\u1ea7u, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i c\u1ee7a \u1ee9ng d\u1ee5ng<\/b><\/td>\n<td><span style=\"font-weight: 400;\">K\u00e9m h\u01a1n khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian, c\u00f3 th\u1ec3 g\u00e2y &#8220;\u0111\u1ee9ng&#8221; UI.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed1t h\u01a1n nhi\u1ec1u, \u1ee9ng d\u1ee5ng v\u1eabn ph\u1ea3n h\u1ed3i trong khi ch\u1edd c\u00e1c t\u00e1c v\u1ee5 ho\u00e0n th\u00e0nh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Hi\u1ec7u su\u1ea5t cho c\u00e1c t\u00e1c v\u1ee5 kh\u00e1c nhau<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c t\u00e1c v\u1ee5 t\u00ednh to\u00e1n nhanh v\u00e0 kh\u00f4ng li\u00ean quan \u0111\u1ebfn I\/O.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed1i \u01b0u cho c\u00e1c t\u00e1c v\u1ee5 li\u00ean quan \u0111\u1ebfn I\/O (network, file, database), gi\u00fap tr\u00e1nh l\u00e3ng ph\u00ed t\u00e0i nguy\u00ean khi ch\u1edd \u0111\u1ee3i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01a1n gi\u1ea3n h\u01a1n do lu\u1ed3ng th\u1ef1c thi tu\u1ea7n t\u1ef1.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ph\u1ee9c t\u1ea1p h\u01a1n, c\u1ea7n qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i khi c\u00e1c t\u00e1c v\u1ee5 ho\u00e0n th\u00e0nh kh\u00f4ng theo th\u1ee9 t\u1ef1.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>X\u1eed l\u00fd l\u1ed7i<\/b><\/td>\n<td><span style=\"font-weight: 400;\">X\u1eed l\u00fd l\u1ed7i th\u01b0\u1eddng tr\u1ef1c quan h\u01a1n (s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\"> th\u00f4ng th\u01b0\u1eddng).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u1ea7n x\u1eed l\u00fd l\u1ed7i m\u1ed9t c\u00e1ch r\u00f5 r\u00e0ng b\u00ean trong c\u00e1c h\u00e0m <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c th\u00f4ng qua c\u00e1c <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Kh\u1ea3 n\u0103ng \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec code<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Code \u0111\u1ed3ng b\u1ed9 th\u01b0\u1eddng d\u1ec5 \u0111\u1ecdc v\u00e0 theo d\u00f5i h\u01a1n \u0111\u1ed1i v\u1edbi c\u00e1c t\u00e1c v\u1ee5 \u0111\u01a1n gi\u1ea3n.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Code b\u1ea5t \u0111\u1ed3ng b\u1ed9, \u0111\u1eb7c bi\u1ec7t khi l\u1ed3ng gh\u00e9p nhi\u1ec1u <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\">, c\u00f3 th\u1ec3 kh\u00f3 \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec h\u01a1n n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c vi\u1ebft c\u1ea9n th\u1eadn. Tuy nhi\u00ean, <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> gi\u00fap c\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Khi_nao_nen_su_dung_dong_bo_va_khi_nao_nen_su_dung_bat_dong_bo_trong_Dart\"><\/span><b>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng \u0111\u1ed3ng b\u1ed9 v\u00e0 khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong Dart?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c l\u1ef1a ch\u1ecdn gi\u1eefa l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 v\u00e0 b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong Dart \u0111\u00f3ng vai tr\u00f2 then ch\u1ed1t trong vi\u1ec7c x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7u su\u1ea5t cao v\u00e0 m\u01b0\u1ee3t m\u00e0. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 h\u01b0\u1edbng d\u1eabn chi ti\u1ebft h\u01a1n v\u1ec1 th\u1eddi \u0111i\u1ec3m n\u00ean \u00e1p d\u1ee5ng t\u1eebng ph\u01b0\u01a1ng ph\u00e1p:<\/span><\/p>\n<h3><b>S\u1eed d\u1ee5ng l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 khi<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00e1c t\u00e1c v\u1ee5 \u0111\u01a1n gi\u1ea3n v\u00e0 ho\u00e0n th\u00e0nh r\u1ea5t nhanh ch\u00f3ng:<\/b><span style=\"font-weight: 400;\"> L\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 ho\u1ea1t \u0111\u1ed9ng t\u1ed1t cho c\u00e1c t\u00e1c v\u1ee5 t\u00ednh to\u00e1n \u0111\u01a1n gi\u1ea3n, c\u00e1c ph\u00e9p to\u00e1n s\u1ed1 h\u1ecdc, ho\u1eb7c c\u00e1c thao t\u00e1c x\u1eed l\u00fd d\u1eef li\u1ec7u nh\u1ecf di\u1ec5n ra g\u1ea7n nh\u01b0 t\u1ee9c th\u00ec. V\u00ed d\u1ee5, vi\u1ec7c c\u1ed9ng hai s\u1ed1 nguy\u00ean, \u0111\u1ecbnh d\u1ea1ng m\u1ed9t chu\u1ed7i v\u0103n b\u1ea3n ng\u1eafn, ho\u1eb7c truy c\u1eadp m\u1ed9t ph\u1ea7n t\u1eed trong m\u1ed9t danh s\u00e1ch nh\u1ecf th\u01b0\u1eddng kh\u00f4ng c\u1ea7n \u0111\u1ebfn s\u1ef1 ph\u1ee9c t\u1ea1p c\u1ee7a l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4ng c\u00f3 t\u00e1c v\u1ee5 n\u00e0o li\u00ean quan \u0111\u1ebfn I\/O ho\u1eb7c network c\u00f3 th\u1ec3 g\u00e2y &#8220;\u0111\u1ee9ng&#8221; \u1ee9ng d\u1ee5ng:<\/b><span style=\"font-weight: 400;\"> N\u1ebfu \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n kh\u00f4ng th\u1ef1c hi\u1ec7n b\u1ea5t k\u1ef3 thao t\u00e1c n\u00e0o c\u00f3 th\u1ec3 m\u1ea5t th\u1eddi gian \u0111\u00e1ng k\u1ec3 nh\u01b0 \u0111\u1ecdc ho\u1eb7c ghi d\u1eef li\u1ec7u t\u1eeb \u1ed5 c\u1ee9ng, g\u1eedi ho\u1eb7c nh\u1eadn d\u1eef li\u1ec7u qua m\u1ea1ng, ho\u1eb7c t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u00e1c d\u1ecbch v\u1ee5 b\u00ean ngo\u00e0i, th\u00ec l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 l\u00e0 l\u1ef1a ch\u1ecdn \u0111\u01a1n gi\u1ea3n v\u00e0 hi\u1ec7u qu\u1ea3. Trong nh\u1eefng tr\u01b0\u1eddng h\u1ee3p n\u00e0y, th\u1eddi gian ch\u1edd \u0111\u1ee3i (n\u1ebfu c\u00f3) th\u01b0\u1eddng r\u1ea5t ng\u1eafn v\u00e0 kh\u00f4ng g\u00e2y \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 hi\u1ec3u c\u1ee7a code \u0111\u01b0\u1ee3c \u01b0u ti\u00ean h\u00e0ng \u0111\u1ea7u:<\/b><span style=\"font-weight: 400;\"> Code \u0111\u1ed3ng b\u1ed9 th\u01b0\u1eddng d\u1ec5 \u0111\u1ecdc, d\u1ec5 vi\u1ebft v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n so v\u1edbi code b\u1ea5t \u0111\u1ed3ng b\u1ed9. N\u1ebfu hi\u1ec7u su\u1ea5t kh\u00f4ng ph\u1ea3i l\u00e0 y\u1ebfu t\u1ed1 then ch\u1ed1t v\u00e0 t\u00e1c v\u1ee5 c\u1ea7n th\u1ef1c hi\u1ec7n r\u1ea5t \u0111\u01a1n gi\u1ea3n, vi\u1ec7c s\u1eed d\u1ee5ng l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 gi\u00fap gi\u1ea3m \u0111\u1ed9 ph\u1ee9c t\u1ea1p c\u1ee7a codebase.<\/span><\/li>\n<\/ul>\n<h3><b>S\u1eed d\u1ee5ng l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 khi<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian nh\u01b0 \u0111\u1ecdc\/ghi file, g\u1ecdi API, t\u01b0\u01a1ng t\u00e1c v\u1edbi database:<\/b><span style=\"font-weight: 400;\"> \u0110\u00e2y l\u00e0 nh\u1eefng t\u00ecnh hu\u1ed1ng \u0111i\u1ec3n h\u00ecnh m\u00e0 l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 ph\u00e1t huy t\u1ed1i \u0111a s\u1ee9c m\u1ea1nh. C\u00e1c thao t\u00e1c n\u00e0y th\u01b0\u1eddng m\u1ea5t m\u1ed9t kho\u1ea3ng th\u1eddi gian kh\u00f4ng x\u00e1c \u0111\u1ecbnh \u0111\u1ec3 ho\u00e0n th\u00e0nh, v\u00e0 vi\u1ec7c &#8220;ch\u1edd \u0111\u1ee3i&#8221; ch\u00fang m\u1ed9t c\u00e1ch \u0111\u1ed3ng b\u1ed9 s\u1ebd khi\u1ebfn \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n b\u1ecb &#8220;\u0111\u1ee9ng&#8221;, kh\u00f4ng ph\u1ea3n h\u1ed3i cho \u0111\u1ebfn khi t\u00e1c v\u1ee5 ho\u00e0n t\u1ea5t. L\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 cho ph\u00e9p \u1ee9ng d\u1ee5ng ti\u1ebfp t\u1ee5c th\u1ef1c hi\u1ec7n c\u00e1c c\u00f4ng vi\u1ec7c kh\u00e1c trong khi ch\u1edd c\u00e1c t\u00e1c v\u1ee5 n\u00e0y ho\u00e0n th\u00e0nh \u1edf n\u1ec1n.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>V\u00ed d\u1ee5:<\/b><span style=\"font-weight: 400;\"> Khi \u1ee9ng d\u1ee5ng c\u1ea7n t\u1ea3i d\u1eef li\u1ec7u ng\u01b0\u1eddi d\u00f9ng t\u1eeb m\u1ed9t API, vi\u1ec7c s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> s\u1ebd gi\u00fap giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng kh\u00f4ng b\u1ecb \u0111\u00f3ng b\u0103ng trong qu\u00e1 tr\u00ecnh t\u1ea3i d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u1ea7n duy tr\u00ec kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i c\u1ee7a \u1ee9ng d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng:<\/b><span style=\"font-weight: 400;\"> Trong c\u00e1c \u1ee9ng d\u1ee5ng c\u00f3 giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng (v\u00ed d\u1ee5: \u1ee9ng d\u1ee5ng di \u0111\u1ed9ng ho\u1eb7c web), vi\u1ec7c gi\u1eef cho giao di\u1ec7n lu\u00f4n ph\u1ea3n h\u1ed3i nhanh ch\u00f3ng l\u00e0 v\u00f4 c\u00f9ng quan tr\u1ecdng \u0111\u1ec3 mang l\u1ea1i tr\u1ea3i nghi\u1ec7m t\u1ed1t cho ng\u01b0\u1eddi d\u00f9ng. B\u1ea5t k\u1ef3 t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian n\u00e0o \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n m\u1ed9t c\u00e1ch \u0111\u1ed3ng b\u1ed9 tr\u00ean lu\u1ed3ng ch\u00ednh c\u1ee7a giao di\u1ec7n \u0111\u1ec1u c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn t\u00ecnh tr\u1ea1ng &#8220;lag&#8221; ho\u1eb7c &#8220;app not responding&#8221;. L\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 gi\u00fap gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 n\u00e0y b\u1eb1ng c\u00e1ch th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1eb7ng nh\u1ecdc \u1edf n\u1ec1n m\u00e0 kh\u00f4ng l\u00e0m gi\u00e1n \u0111o\u1ea1n lu\u1ed3ng ch\u00ednh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u1eed l\u00fd c\u00e1c s\u1ef1 ki\u1ec7n ho\u1eb7c lu\u1ed3ng d\u1eef li\u1ec7u theo th\u1eddi gian:<\/b><span style=\"font-weight: 400;\"> Khi \u1ee9ng d\u1ee5ng c\u1ea7n x\u1eed l\u00fd c\u00e1c s\u1ef1 ki\u1ec7n x\u1ea3y ra kh\u00f4ng \u0111\u1ed3ng th\u1eddi ho\u1eb7c l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c lu\u1ed3ng d\u1eef li\u1ec7u li\u00ean t\u1ee5c (v\u00ed d\u1ee5: d\u1eef li\u1ec7u t\u1eeb c\u1ea3m bi\u1ebfn, th\u00f4ng b\u00e1o \u0111\u1ea9y), <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> trong Dart l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd \u0111\u1ec3 qu\u1ea3n l\u00fd c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 n\u00e0y m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_thuong_gap_ve_so_sanh_lap_trinh_asynchronous_va_synchronous_trong_Dart\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>async<\/b><b> v\u00e0 <\/b><b>await<\/b><b> c\u00f3 vai tr\u00f2 g\u00ec trong Dart? Ch\u00fang c\u00f3 ph\u1ea3i l\u00e0 lu\u1ed3ng (threads) kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\"> \u0111\u00e1nh d\u1ea5u m\u1ed9t h\u00e0m l\u00e0 b\u1ea5t \u0111\u1ed3ng b\u1ed9, cho ph\u00e9p s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> b\u00ean trong n\u00f3. <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> s\u1ebd t\u1ea1m d\u1eebng vi\u1ec7c th\u1ef1c thi h\u00e0m cho \u0111\u1ebfn khi <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> m\u00e0 n\u00f3 \u0111ang ch\u1edd \u0111\u1ee3i ho\u00e0n th\u00e0nh, nh\u01b0ng n\u00f3 kh\u00f4ng ch\u1eb7n lu\u1ed3ng ch\u00ednh c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Dart s\u1eed d\u1ee5ng m\u1ed9t m\u00f4 h\u00ecnh \u0111\u01a1n lu\u1ed3ng v\u1edbi m\u1ed9t v\u00f2ng l\u1eb7p s\u1ef1 ki\u1ec7n (event loop) \u0111\u1ec3 qu\u1ea3n l\u00fd c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9, ch\u1ee9 kh\u00f4ng t\u1ea1o ra c\u00e1c lu\u1ed3ng ri\u00eang bi\u1ec7t nh\u01b0 trong \u0111a lu\u1ed3ng (m\u1eb7c d\u00f9 Dart c\u0169ng h\u1ed7 tr\u1ee3 Isolates cho concurrency).<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/dart-tutorial\/\" target=\"_blank\" rel=\"noopener\"><strong>Dart tutorial: C\u00e1c b\u01b0\u1edbc \u0111\u1ea7u ti\u00ean l\u00e0m quen v\u1edbi l\u1eadp tr\u00ecnh Dart<\/strong><\/a><\/em><\/p><\/blockquote>\n<h3><b>Future<\/b><b> trong Dart \u0111\u1ea1i di\u1ec7n cho \u0111i\u1ec1u g\u00ec? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 l\u1ea5y \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 t\u1eeb m\u1ed9t <\/b><b>Future<\/b><b>?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho k\u1ebft qu\u1ea3 c\u1ee7a m\u1ed9t ph\u00e9p t\u00ednh b\u1ea5t \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 ho\u00e0n th\u00e0nh trong t\u01b0\u01a1ng lai. B\u1ea1n c\u00f3 th\u1ec3 l\u1ea5y k\u1ebft qu\u1ea3 t\u1eeb m\u1ed9t <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">await<\/span><span style=\"font-weight: 400;\"> b\u00ean trong m\u1ed9t h\u00e0m <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">, ho\u1eb7c b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">then()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0nh \u0111\u1ed9ng khi <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> ho\u00e0n th\u00e0nh v\u00e0 <\/span><span style=\"font-weight: 400;\">catchError()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i.<\/span><\/p>\n<h3><b>Hi\u1ec7u su\u1ea5t c\u1ee7a l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 v\u00e0 b\u1ea5t \u0111\u1ed3ng b\u1ed9 kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o trong Dart?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">L\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 nhanh h\u01a1n cho c\u00e1c t\u00e1c v\u1ee5 r\u1ea5t ng\u1eafn v\u00e0 kh\u00f4ng c\u00f3 I\/O. Tuy nhi\u00ean, \u0111\u1ed1i v\u1edbi c\u00e1c t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian, l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 gi\u00fap \u1ee9ng d\u1ee5ng duy tr\u00ec kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i t\u1ed1t h\u01a1n b\u1eb1ng c\u00e1ch kh\u00f4ng ch\u1eb7n lu\u1ed3ng ch\u00ednh. \u0110i\u1ec1u n\u00e0y c\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3 tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng.<\/span><\/p>\n<h3><b>T\u00f4i n\u00ean b\u1eaft \u0111\u1ea7u t\u00ecm hi\u1ec3u v\u1ec1 l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong Dart t\u1eeb \u0111\u00e2u?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n n\u00ean b\u1eaft \u0111\u1ea7u v\u1edbi <a href=\"https:\/\/www.google.com\/search?q=https:\/\/dart.dev\/guides\/language\/language-tour%23asynchrony-support&amp;authuser=1\" target=\"_blank\" rel=\"noopener\">t\u00e0i li\u1ec7u ch\u00ednh th\u1ee9c c\u1ee7a Dart v\u1ec1 Asynchrony Support<\/a><\/span><span style=\"font-weight: 400;\">.<\/span> <span style=\"font-weight: 400;\">Sau \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 <\/span><span style=\"font-weight: 400;\">Future<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> trong t\u00e0i li\u1ec7u API c\u1ee7a Dart. C\u00e1c kh\u00f3a h\u1ecdc v\u00e0 b\u00e0i vi\u1ebft tr\u1ef1c tuy\u1ebfn c\u0169ng l\u00e0 ngu\u1ed3n t\u00e0i li\u1ec7u h\u1eefu \u00edch.<\/span><\/p>\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;\">\u0110\u1ec3 so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart, b\u1ea1n c\u00f3 th\u1ec3 nh\u1edb r\u1eb1ng:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">\u0110\u1ed3ng b\u1ed9 cho t\u00e1c v\u1ee5 nhanh<\/span><\/li>\n<li><span style=\"font-weight: 400;\">B\u1ea5t \u0111\u1ed3ng b\u1ed9 cho t\u00e1c v\u1ee5 t\u1ed1n th\u1eddi gian \u0111\u1ec3 \u1ee9ng d\u1ee5ng lu\u00f4n m\u01b0\u1ee3t m\u00e0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Hi\u1ec3u r\u00f5 s\u1ef1 kh\u00e1c bi\u1ec7t v\u00e0 v\u1eadn d\u1ee5ng linh ho\u1ea1t gi\u1eefa l\u1eadp tr\u00ecnh \u0111\u1ed3ng b\u1ed9 v\u00e0 b\u1ea5t \u0111\u1ed3ng b\u1ed9 l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng quan tr\u1ecdng \u0111\u1ec3 tr\u1edf th\u00e0nh m\u1ed9t l\u1eadp tr\u00ecnh vi\u00ean Dart th\u00e0nh th\u1ea1o, gi\u00fap b\u1ea1n x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng m\u1ea1nh m\u1ebd, hi\u1ec7u su\u1ea5t cao v\u00e0 mang l\u1ea1i tr\u1ea3i nghi\u1ec7m t\u1ed1t nh\u1ea5t cho ng\u01b0\u1eddi d\u00f9ng. H\u00e3y l\u1ef1a ch\u1ecdn ph\u00f9 h\u1ee3p t\u00f9y thu\u1ed9c v\u00e0o t\u1eebng t\u00ecnh hu\u1ed1ng v\u00e0 th\u1ef1c h\u00e0nh c\u1ea3 hai \u0111\u1ec3 tr\u1edf th\u00e0nh l\u1eadp tr\u00ecnh vi\u00ean Dart gi\u1ecfi nh\u00e9.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>B\u1ea1n \u0111\u00e3 bao gi\u1edd t\u1ef1 h\u1ecfi t\u1ea1i sao m\u1ed9t s\u1ed1 \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng m\u01b0\u1ee3t m\u00e0 ngay c\u1ea3 khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1eb7ng, trong khi nh\u1eefng \u1ee9ng d\u1ee5ng kh\u00e1c l\u1ea1i c\u00f3 v\u1ebb &#8220;\u0111\u1ee9ng h\u00ecnh&#8221; v\u00e0 ch\u1eadm ch\u1ea1p? B\u00ed m\u1eadt n\u1eb1m \u1edf c\u00e1ch ch\u00fang qu\u1ea3n l\u00fd c\u00e1c t\u00e1c v\u1ee5, \u0111\u1eb7c bi\u1ec7t l\u00e0 s\u1ef1 kh\u00e1c [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":86366,"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-86362","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>So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Chi ti\u1ebft so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart \u0111\u1ec3 gi\u00fap vi\u1ebft app Dart m\u01b0\u1ee3t, hi\u1ec7u su\u1ea5t cao v\u00e0 tr\u1ea3i nghi\u1ec7m t\u1ed1i \u01b0u.\" \/>\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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart\" \/>\n<meta property=\"og:description\" content=\"B\u1ea1n \u0111\u00e3 bao gi\u1edd t\u1ef1 h\u1ecfi t\u1ea1i sao m\u1ed9t s\u1ed1 \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng m\u01b0\u1ee3t m\u00e0 ngay c\u1ea3 khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1eb7ng, trong khi nh\u1eefng \u1ee9ng d\u1ee5ng kh\u00e1c l\u1ea1i c\u00f3 v\u1ebb &quot;\u0111\u1ee9ng h\u00ecnh&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-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-26T11:11:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-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=\"21 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart - ITviec Blog","description":"Chi ti\u1ebft so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart \u0111\u1ec3 gi\u00fap vi\u1ebft app Dart m\u01b0\u1ee3t, hi\u1ec7u su\u1ea5t cao v\u00e0 tr\u1ea3i nghi\u1ec7m t\u1ed1i \u01b0u.","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\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/","og_locale":"vi_VN","og_type":"article","og_title":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart","og_description":"B\u1ea1n \u0111\u00e3 bao gi\u1edd t\u1ef1 h\u1ecfi t\u1ea1i sao m\u1ed9t s\u1ed1 \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng m\u01b0\u1ee3t m\u00e0 ngay c\u1ea3 khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1eb7ng, trong khi nh\u1eefng \u1ee9ng d\u1ee5ng kh\u00e1c l\u1ea1i c\u00f3 v\u1ebb \"\u0111\u1ee9ng h\u00ecnh\"","og_url":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-04-26T11:11:09+00:00","og_image":[{"width":640,"height":337,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-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":"21 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/"},"author":{"name":"Tien Tran","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298"},"headline":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart","datePublished":"2025-04-26T11:11:09+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/"},"wordCount":5822,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart-vippro-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/","url":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/","name":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart-vippro-scaled.png","datePublished":"2025-04-26T11:11:09+00:00","description":"Chi ti\u1ebft so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart \u0111\u1ec3 gi\u00fap vi\u1ebft app Dart m\u01b0\u1ee3t, hi\u1ec7u su\u1ea5t cao v\u00e0 tr\u1ea3i nghi\u1ec7m t\u1ed1i \u01b0u.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart-vippro-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/04\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-dart-vippro-scaled.png","width":640,"height":337,"caption":"so s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong dart - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/so-sanh-lap-trinh-asynchronous-va-synchronous-trong-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":"So s\u00e1nh l\u1eadp tr\u00ecnh asynchronous v\u00e0 synchronous trong Dart"}]},{"@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\/86362","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=86362"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/86362\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/86366"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=86362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=86362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=86362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}