Thuta Learning
Flutter
IntermediateMobile Developmentအခြေခံ

State Management Intro

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

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

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

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

State Management Intro ကို ရိုးရိုးလေးနားလည်မယ်

State ဆိုတာ app ရဲ့ လက်ရှိအခြေအနေပါ။ ဥပမာ counter number, login user, selected tab, cart item count, API result, loading/error status — အားလုံး state ဖြစ်နိုင်ပါတယ်။ State Management ဆိုတာ ဒီအခြေအနေတွေကို ဘယ်နေရာမှာသိမ်းမလဲ၊ ဘယ်လိုပြောင်းမလဲ၊ UI ကိုဘယ်လို update လုပ်မလဲဆိုတာ စီမံခြင်းပါ။ အစပိုင်းမှာ setState() နဲ့စတာအကောင်းဆုံးပါ။ Project ကြီးလာရင် Provider, Riverpod, BLoC, GetX စတာတွေကိုရွေးချယ်နိုင်ပါတယ်။ သို့သော် အခြေခံကိုမသိဘဲ advanced package တက်သွားရင် မုန့်ဟင်းခါးမသိဘဲ molecular gastronomy ချက်သလိုဖြစ်တတ်ပါတယ်။

Problem → Why → Solution

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

State ကိုနေရာတိုင်းမှာကော်ပီကူးသိမ်းထားရင် data မတူတော့တဲ့ bug တွေဖြစ်တတ်ပါတယ်။ Single source of truth ကိုစဉ်းစားပါ။

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

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

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

State Management Intro မှာ source of truth, validation, focus, controller disposal နဲ့ minimal rebuild boundary ကိုရှင်းပါ။ Ephemeral state ကို local ထားပြီး shared app state ကို testable controller/model တစ်ခုကပိုင်ဆိုင်စေပါ။

Mental Model
  1. Event
  2. State owner + notifier
  3. Rebuilt widgets

State Management Intro ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

dart
// Simple state examples
int counter = 0;
bool isLoading = false;
String selectedCategory = 'All';
List<String> cartItems = [];

State Management Intro မှာ source of truth, validation, focus, controller disposal နဲ့ minimal rebuild boundary ကိုရှင်းပါ။ Ephemeral state ကို local ထားပြီး shared app state ကို testable controller/model တစ်ခုကပိုင်ဆိုင်စေပါ။

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

  1. Event// Simple state examples

    State ဆိုတာ app ရဲ့ လက်ရှိအခြေအနေပါ။ ဥပမာ counter number, login user, selected tab, cart item count, API result, loading/error status — အားလုံး state ဖြစ်နိုင်ပါတယ်။ State Management ဆိုတာ ဒီအခြေအနေတွေကို ဘယ်နေရာမှာသိမ်းမလဲ၊ ဘယ်လိုပြောင်းမလဲ၊ UI ကိုဘယ်လို update လုပ်မလဲဆိုတာ စီမံခြင်းပါ။ အစပိုင်းမှာ setState() နဲ့စတာအကောင်းဆုံးပါ။ Project ကြီးလာရင် Provider, Riverpod, BLoC, GetX စတာတွေကိုရွေးချယ်နိုင်ပါတယ်။ သို့သော် အခြေခံကိုမသိဘဲ advanced package တက်သွားရင် မုန့်ဟင်းခါးမသိဘဲ molecular gastronomy ချက်သလိုဖြစ်တတ်ပါတယ်။

  2. State owner + notifierbool isLoading = false;

    State Management Intro မှာ source of truth, validation, focus, controller disposal နဲ့ minimal rebuild boundary ကိုရှင်းပါ။ Ephemeral state ကို local ထားပြီး shared app state ကို testable controller/model တစ်ခုကပိုင်ဆိုင်စေပါ။

  3. Rebuilt widgetsList<String> cartItems = [];

    State Management Intro 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. Event
  2. State owner + notifier
  3. Rebuilt widgets

Success

State Management Intro ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

Failure

State ကိုနေရာတိုင်းမှာကော်ပီကူးသိမ်းထားရင် data မတူတော့တဲ့ bug တွေဖြစ်တတ်ပါတယ်။ Single source of truth ကိုစဉ်းစားပါ။

State ဆိုတာ app ရဲ့ လက်ရှိအခြေအနေပါ။ ဥပမာ counter number, login user, selected tab, cart item count, API result, loading/error status — အားလုံး state ဖြစ်နိုင်ပါတယ်။ State Management ဆိုတာ ဒီအခြေအနေတွေကို ဘယ်နေရာမှာသိမ်းမလဲ၊ ဘယ်လိုပြောင်းမလဲ၊ UI ကိုဘယ်လို update လုပ်မလဲဆိုတာ စီမံခြင်းပါ။ အစပိုင်းမှာ setState() နဲ့စတာအကောင်းဆုံးပါ။ Project ကြီးလာရင် Provider, Riverpod, BLoC, GetX စတာတွေကိုရွေးချယ်နိုင်ပါတယ်။ သို့သော် အခြေခံကိုမသိဘဲ advanced package တက်သွားရင် မုန့်ဟင်းခါးမသိဘဲ molecular gastronomy ချက်သလိုဖြစ်တတ်ပါတယ်။

dart
// Simple state examples
int counter = 0;
bool isLoading = false;
String selectedCategory = 'All';
List<String> cartItems = [];
You should see
ဒီ code က UI မပြသေးပါ။ State အမျိုးအစားတွေကိုနားလည်စေရန်ပြထားတဲ့ example ဖြစ်ပါတယ်။

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

State ကို UI နဲ့ချိတ်ပြီးပြောင်းဖို့ setState() lesson ကိုဆက်သင်ပါ။

Real-world usage

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

State Management Intro 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

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

State Management Intro 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
// Simple state examples
int counter = 0;
bool isLoading = false;
String selectedCategory = 'All';
List<String> cartItems = [];

ရလဒ်: State Management Intro ရဲ့ widget, state နဲ့ lifecycle behavior ကိုရှင်းပြနိုင်ရန်

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

State Management Intro 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

State Management Intro ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Event → State owner + notifier → Rebuilt widgets
  • State Management Intro မှာ source of truth, validation, focus, controller disposal နဲ့ minimal rebuild boundary ကိုရှင်းပါ။ Ephemeral state ကို local ထားပြီး shared app state ကို testable controller/model တစ်ခုကပိုင်ဆိုင်စေပါ။
  • State ကိုနေရာတိုင်းမှာကော်ပီကူးသိမ်းထားရင် data မတူတော့တဲ့ bug တွေဖြစ်တတ်ပါတယ်။ Single source of truth ကိုစဉ်းစားပါ။
  • State Management Intro 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

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

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

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: ဒီ code က UI မပြသေးပါ။ State အမျိုးအစားတွေကိုနားလည်စေရန်ပြထားတဲ့ example ဖြစ်ပါတယ်။

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

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

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

State Management Intro | Thuta Learning