ရိုးရိုးလေး ရှင်းပြမယ်
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 ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
- Async request
- Repository / service
- Data / error UI
Fetching Data (http) ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// 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 ကို အဆင့်လိုက်ဖတ်မယ်
- 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 အလိုက်ပြနိုင်ပါတယ်။
- Repository / service
}Fetching Data (http) ကို UI, domain, data source နဲ့ failure state ခွဲထားသော flow အဖြစ်တည်ဆောက်ပါ။ Loading, empty, success, error, retry, cancellation နဲ့ route restoration behavior ကိုရှင်းလင်းစွာကိုင်တွယ်ပါ။
- 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 ကိုလိုအပ်သလိုသုံးပါ။
- Async request
- Repository / service
- 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 အလိုက်ပြနိုင်ပါတယ်။
// 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');
},
)စတင်ချိန်မှာ 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 ကိုခန့်မှန်းပါ
// 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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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