Thuta Learning
Dart
AdvancedProgrammingbeginner

Sealed Types & Error Handling

Dartသင်ခန်းစာ 20

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

  • Sealed Types & Error Handling ရဲ့ type နဲ့ runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Null, failure, boundary နဲ့ async cases စမ်းနိုင်ရန်
  • Safe, idiomatic, testable Dart code ရေးနိုင်ရန်

Sealed Types & Error Handling ကို stable API နဲ့ exhaustive domain model ဖော်ပြဖို့သုံးပါ။ Composition, sealed hierarchies, constrained generics နဲ့ focused extensions ကိုဦးစားပေးပြီး hidden mutation နဲ့ unchecked casts ကိုလျှော့ပါ။

ပိုပြီးနားလည်ထားရမယ့် အချက်

Sealed Types & Error Handling ကို stable API နဲ့ exhaustive domain model ဖော်ပြဖို့သုံးပါ။ Composition, sealed hierarchies, constrained generics နဲ့ focused extensions ကိုဦးစားပေးပြီး hidden mutation နဲ့ unchecked casts ကိုလျှော့ပါ။

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

Sealed Types & Error Handling example ကို null, empty, boundary, invalid, async-error နဲ့ cancellation cases ဖြင့် run/test လုပ်ပါ။ dart format, dart analyze နဲ့ dart test ကိုသုံးပြီး Future/Stream subscription နဲ့ isolate boundary ကိုလိုအပ်သလိုစစ်ပါ။

ဒီသင်ခန်းစာပြီးရင်

dart
sealed class LoadState {}
class Loading extends LoadState {}
class Ready extends LoadState { Ready(this.items); final List<String> items; }
class Failed extends LoadState { Failed(this.message); final String message; }

String label(LoadState state) => switch (state) {
  Loading() => 'Loading',
  Ready(items: final items) => 'Ready: ${items.length}',
  Failed(message: final message) => 'Error: $message',
};

void main() => print(label(Ready(['A', 'B'])));
You should see
Ready: 2

ကိုယ်တိုင်စမ်းကြည့်ရန်

Sealed Types & Error Handling example ကို null, empty, boundary, invalid, async-error နဲ့ cancellation cases ဖြင့် run/test လုပ်ပါ။ dart format, dart analyze နဲ့ dart test ကိုသုံးပြီး Future/Stream subscription နဲ့ isolate boundary ကိုလိုအပ်သလိုစစ်ပါ။

Null/Async သတိပေးချက်

Sample output တစ်ခုမှန်ရုံဖြင့် null, promotion, Future, Stream နဲ့ isolate paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

Dart class modifiersDart

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

  • Sample output တစ်ခုမှန်ရုံဖြင့် null, promotion, Future, Stream နဲ့ isolate paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • dynamic, ! သို့မပိတ်ထားသော Stream subscription ဖြင့် type safety နဲ့ resource lifecycle ကိုကျော်ခြင်း။

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

Sealed Types & Error Handling example ကို null, empty, boundary, invalid, async-error နဲ့ cancellation cases ဖြင့် run/test လုပ်ပါ။ dart format, dart analyze နဲ့ dart test ကိုသုံးပြီး Future/Stream subscription နဲ့ isolate boundary ကိုလိုအပ်သလိုစစ်ပါ။

You'll know it worked when: Ready: 2

Sealed Types & Error Handling | Thuta Learning