Thuta Learning
Flutter
AdvancedMobile Developmentအခြေခံ

Async: Futures

Flutterသင်ခန်းစာ 2425 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Async: Futures ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
  • Responsive, failure နဲ့ interaction cases စမ်းနိုင်ရန်
  • Accessible, testable, performant Flutter code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။

Mental Model
  1. Async request
  2. Repository / service
  3. Data / error UI

Async: Futures ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

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');
}

Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Async requestFuture<String> fetchUserName() async {

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

  2. Repository / servicevoid main() async {

    Async: Futures ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။

  3. 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 ကိုလိုအပ်သလိုသုံးပါ။

Execution Flow
  1. Async request
  2. Repository / service
  3. 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 ဖြစ်ပါတယ်။

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 လုပ်တာကိုလေ့လာပါ။

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 ကိုခန့်မှန်းပါ
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');
}

ရလဒ်: 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

Async: Futures ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

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

  • await မသုံးဘဲ Future ကိုတန်း print လုပ်ရင် Instance of Future ဆိုတာမျိုးပေါ်တတ်ပါတယ်။ Result လိုချင်ရင် await သုံးပါ။
  • Screen တစ်မျိုးတွင် sample output မှန်ရုံဖြင့် rebuild, overflow, accessibility, async နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • build() ထဲ expensive work လုပ်ခြင်း၊ controller/subscription မပိတ်ခြင်း သို့ setState ကိုကျယ်သော subtree ပေါ်ခေါ်ခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

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 ကိုလိုအပ်သလိုသုံးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Console မှာ Loading user... ပေါ်ပြီး ၂ စက္ကန့်နောက်မှာ Hello, Sai ပေါ်ပါမယ်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Async: Futures | Thuta Learning