Thuta Learning
ရှာဖွေရန်
AdvancedMobile Developmentbeginner

Async: Futures

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

App တစ်ခုမှာ data အားလုံးက ချက်ချင်းမရနိုင်ပါ။ API call, file read, database query, location request တွေက အချိန်ယူပါတယ်။ Dart မှာ Future က နောက်မှရလာမယ့် value ကိုကိုယ်စားပြုပါတယ်။ async/await က အချိန်ယူတဲ့ code ကို ဖတ်ရလွယ်အောင်ရေးပေးတဲ့ syntax ဖြစ်ပါတယ်။

dart
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');
}
You should see
Console မှာ Loading user... ပေါ်ပြီး ၂ စက္ကန့်နောက်မှာ Hello, Sai ပေါ်ပါမယ်။

နောက်တစ်ဆင့်

Async အခြေခံရပြီဆိုရင် HTTP package နဲ့ API data fetch လုပ်တာကိုလေ့လာပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • await မသုံးဘဲ Future ကိုတန်း print လုပ်ရင် Instance of Future ဆိုတာမျိုးပေါ်တတ်ပါတယ်။ Result လိုချင်ရင် await သုံးပါ။
Async: Futures | Thuta Learning