ရိုးရိုးလေး ရှင်းပြမယ်
Async: Futures ကို ရိုးရိုးလေးနားလည်မယ်
App တစ်ခုမှာ data အားလုံးက ချက်ချင်းမရနိုင်ပါ။ API call, file read, database query, location request တွေက အချိန်ယူပါတယ်။ Dart မှာ Future က နောက်မှရလာမယ့် value ကိုကိုယ်စားပြုပါတယ်။ async/await က အချိန်ယူတဲ့ code ကို ဖတ်ရလွယ်အောင်ရေးပေးတဲ့ syntax ဖြစ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
await မသုံးဘဲ Future ကိုတန်း print လုပ်ရင် Instance of Future ဆိုတာမျိုးပေါ်တတ်ပါတယ်။ Result လိုချင်ရင် await သုံးပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Screen တစ်မျိုးတွင် sample output မှန်ရုံဖြင့် rebuild, overflow, accessibility, async နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
- Async request
- Repository / service
- Data / error UI
Async: Futures ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
Future<String> fetchUserName() async {
await Future.delayed(const Duration(seconds: 2));
return 'Sai';
}
void main() async {
print('Loading user...');
final name = await fetchUserName();
print('Hello, $name');
}Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Async request
Future<String> fetchUserName() async {App တစ်ခုမှာ data အားလုံးက ချက်ချင်းမရနိုင်ပါ။ API call, file read, database query, location request တွေက အချိန်ယူပါတယ်။ Dart မှာ Future က နောက်မှရလာမယ့် value ကိုကိုယ်စားပြုပါတယ်။ async/await က အချိန်ယူတဲ့ code ကို ဖတ်ရလွယ်အောင်ရေးပေးတဲ့ syntax ဖြစ်ပါတယ်။
- Repository / service
void main() async {Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
- Data / error UI
}Async: Futures example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။
- Async request
- Repository / service
- Data / error UI
Success
Async: Futures ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
Failure
await မသုံးဘဲ Future ကိုတန်း print လုပ်ရင် Instance of Future ဆိုတာမျိုးပေါ်တတ်ပါတယ်။ Result လိုချင်ရင် await သုံးပါ။
App တစ်ခုမှာ data အားလုံးက ချက်ချင်းမရနိုင်ပါ။ API call, file read, database query, location request တွေက အချိန်ယူပါတယ်။ Dart မှာ Future က နောက်မှရလာမယ့် value ကိုကိုယ်စားပြုပါတယ်။ async/await က အချိန်ယူတဲ့ code ကို ဖတ်ရလွယ်အောင်ရေးပေးတဲ့ syntax ဖြစ်ပါတယ်။
Future<String> fetchUserName() async {
await Future.delayed(const Duration(seconds: 2));
return 'Sai';
}
void main() async {
print('Loading user...');
final name = await fetchUserName();
print('Hello, $name');
}Console မှာ Loading user... ပေါ်ပြီး ၂ စက္ကန့်နောက်မှာ Hello, Sai ပေါ်ပါမယ်။နောက်တစ်ဆင့်
Async အခြေခံရပြီဆိုရင် HTTP package နဲ့ API data fetch လုပ်တာကိုလေ့လာပါ။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Async: Futures example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Async: Futures example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
Future<String> fetchUserName() async {
await Future.delayed(const Duration(seconds: 2));
return 'Sai';
}
void main() async {
print('Loading user...');
final name = await fetchUserName();
print('Hello, $name');
}ရလဒ်: Async: Futures ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Async: Futures example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Async request → Repository / service → Data / error UI
- Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
- await မသုံးဘဲ Future ကိုတန်း print လုပ်ရင် Instance of Future ဆိုတာမျိုးပေါ်တတ်ပါတယ်။ Result လိုချင်ရင် await သုံးပါ။
- Async: Futures example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။
Flutter documentation — Flutter