Thuta Learning
Flutter
AdvancedMobile Developmentအခြေခံ

Fetching Data (http)

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

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

  • Fetching Data (http) ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
  • Responsive, failure နဲ့ interaction cases စမ်းနိုင်ရန်
  • Accessible, testable, performant Flutter code ရေးနိုင်ရန်

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

Fetching Data (http) ကို ရိုးရိုးလေးနားလည်မယ်

Flutter app တွေမှာ backend API, public API, database service တွေက data ကိုယူရတာများပါတယ်။ http package ကိုသုံးပြီး GET/POST request လုပ်နိုင်ပါတယ်။ UI ဘက်မှာ FutureBuilder ကိုသုံးရင် loading, success, error state များကို condition အလိုက်ပြနိုင်ပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Internet permission, package import, dependency version, API response format စတာတွေမစစ်ဘဲ error ကို Flutter bug လို့ထင်တတ်ပါတယ်။ Console log ကိုဖတ်ပါ။

ဖြစ်လေ့ရှိတဲ့အမှား

Screen တစ်မျိုးတွင် sample output မှန်ရုံဖြင့် rebuild, overflow, accessibility, async နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Fetching Data (http) ကို 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

Fetching Data (http) ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

dart
// pubspec.yaml ထဲမှာ dependency ထည့်ပါ
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)

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

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

  1. Async request// pubspec.yaml ထဲမှာ dependency ထည့်ပါ

    Flutter app တွေမှာ backend API, public API, database service တွေက data ကိုယူရတာများပါတယ်။ http package ကိုသုံးပြီး GET/POST request လုပ်နိုင်ပါတယ်။ UI ဘက်မှာ FutureBuilder ကိုသုံးရင် loading, success, error state များကို condition အလိုက်ပြနိုင်ပါတယ်။

  2. Repository / service}

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

  3. Data / error UI)

    Fetching Data (http) 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

Fetching Data (http) ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

Failure

Internet permission, package import, dependency version, API response format စတာတွေမစစ်ဘဲ error ကို Flutter bug လို့ထင်တတ်ပါတယ်။ Console log ကိုဖတ်ပါ။

Flutter app တွေမှာ backend API, public API, database service တွေက data ကိုယူရတာများပါတယ်။ http package ကိုသုံးပြီး GET/POST request လုပ်နိုင်ပါတယ်။ UI ဘက်မှာ FutureBuilder ကိုသုံးရင် loading, success, error state များကို condition အလိုက်ပြနိုင်ပါတယ်။

dart
// pubspec.yaml ထဲမှာ dependency ထည့်ပါ
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)
You should see
စတင်ချိန်မှာ loading spinner ပေါ်ပြီး data ရလာတဲ့အခါ title စာသားပေါ်ပါမယ်။ Error ဖြစ်ရင် error message ပေါ်ပါမယ်။

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

နောက်ဆုံး Mini Project lesson မှာ Flutter concept အများစုကိုပေါင်းပြီး small app တစ်ခုတည်ဆောက်ကြည့်ပါ။

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Fetching Data (http) 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

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Fetching Data (http) 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
// pubspec.yaml ထဲမှာ dependency ထည့်ပါ
// dependencies:
//   http: ^1.2.0

Future<String> fetchTitle() async {
  final response = await http.get(
    Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    return data['title'];
  }

  throw Exception('Failed to load title');
}

FutureBuilder<String>(
  future: fetchTitle(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const CircularProgressIndicator();
    }

    if (snapshot.hasError) {
      return Text('Error: ${snapshot.error}');
    }

    return Text(snapshot.data ?? 'No title');
  },
)

ရလဒ်: Fetching Data (http) ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Fetching Data (http) 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

Fetching Data (http) ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Async request → Repository / service → Data / error UI
  • Fetching Data (http) ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
  • Internet permission, package import, dependency version, API response format စတာတွေမစစ်ဘဲ error ကို Flutter bug လို့ထင်တတ်ပါတယ်။ Console log ကိုဖတ်ပါ။
  • Fetching Data (http) 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

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

  • Internet permission, package import, dependency version, API response format စတာတွေမစစ်ဘဲ error ကို Flutter bug လို့ထင်တတ်ပါတယ်။ Console log ကိုဖတ်ပါ။
  • Screen တစ်မျိုးတွင် sample output မှန်ရုံဖြင့် rebuild, overflow, accessibility, async နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • build() ထဲ expensive work လုပ်ခြင်း၊ controller/subscription မပိတ်ခြင်း သို့ setState ကိုကျယ်သော subtree ပေါ်ခေါ်ခြင်း။

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

Fetching Data (http) example ကို small/large screen, text scaling, empty/loading/error, rapid interaction, navigation restore နဲ့ disposal cases ဖြင့် run/test လုပ်ပါ။ flutter analyze, widget tests နဲ့ profile mode ကိုလိုအပ်သလိုသုံးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: စတင်ချိန်မှာ loading spinner ပေါ်ပြီး data ရလာတဲ့အခါ title စာသားပေါ်ပါမယ်။ Error ဖြစ်ရင် error message ပေါ်ပါမယ်။

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

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

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

Fetching Data (http) | Thuta Learning