Thuta Learning
Dart
ExercisesProgrammingအခြေခံ

Dart Exercises: OOP နဲ့ Async

Dartသင်ခန်းစာ 2940 မိနစ်

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

  • Nullable field ပါတဲ့ base class ကို design လုပ်ပြီး subclass တွေက super နဲ့ default သတ်မှတ်နိုင်ဖို့
  • Mixin ကို on နဲ့ ကန့်သတ်ပြီး with နဲ့ သုံးနိုင်ဖို့
  • Future<T?> ပြန်ပြီး မရှိတဲ့ ရလဒ်နဲ့ ပစ်လိုက်တဲ့ error နှစ်မျိုးလုံးကို ခေါ်တဲ့နေရာမှာ ကိုင်တွယ်နိုင်ဖို့
  • Future.wait နဲ့ future အများကြီးကို တစ်ပြိုင်နက် run ပြီး ရလဒ်တွေက ခေါ်တဲ့အစီအစဉ်အတိုင်း ပြန်လာတယ်ဆိုတာ သိဖို့
  • Type တူတဲ့ argument နှစ်ခုကို တိတ်တိတ်လေး မလဲမိအောင် named parameter သုံးနိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

ဒီ lesson က object-oriented chapter တွေကို null safety နဲ့ async code နဲ့ ပေါင်းထားပါတယ်။ လေ့ကျင့်ခန်း တစ်ခုစီက သေးပေမယ့် တကယ့် application တွေမှာ ဒီအယူအဆတွေက တစ်ခုတည်း မပေါ်ပါဘူး: class hierarchy မှာ မရှိနိုင်တဲ့ field တွေ ပါတတ်ပြီး object တွေက file ဒါမှမဟုတ် server လို နှေးတဲ့ အရာကနေ ရောက်လာတတ်ပါတယ်။

တိရစ္ဆာန်ရုံ registry တစ်ခုကို မြင်ကြည့်ပါ။ Animal က record တိုင်း လိုက်နာရတဲ့ blueprint ပါ။ Dog နဲ့ Bird က အဲဒါကို ဖြည့်ပြီးသား version တွေပါ။ Flyable က တချို့ တိရစ္ဆာန်တွေပဲ တပ်ထားတဲ့ skill badge ပါ။ Record တစ်ခု ယူရတာ အချိန်ကြာပါတယ်၊ ရုံးတစ်ခုခုကနေ လာတာမို့ပါ၊ ရုံးက 'ဒီတိရစ္ဆာန် မရှိဘူး' လို့လည်း ဖြေနိုင်ပါတယ်။ သင့် task တွေက ဒီအခြေအနေတိုင်းမှာ compiler က ကူညီနိုင်အောင် registry ကို model လုပ်ဖို့ပါ။

Problem → Why → Solution

ပြဿနာ: မှန်သလိုပေါ်ပြီး bug သုံးခု ဖုံးထားတဲ့ draft

Registry ရဲ့ ပထမ draft က များသောအားဖြင့် ကောင်းသလို ပေါ်ပြီး bug သုံးခုကို ဖုံးထားပါတယ်။ sound field က nullable ဖြစ်ပြီး field က public ဖြစ်ရင် null check ပြီးနောက် length ကို ဖတ်တာ compile မဖြစ်ပါဘူး။ Dog constructor က name နဲ့ sound ကို အစီအစဉ်ပြောင်းပြီး ပေးထားပါတယ်၊ နှစ်ခုလုံး String မို့ ဘာမှ မတိုင်ဘဲ dog က sound နေရာမှာ သူ့နာမည်ကို ပြောပါတယ်။ Async lookup ကို await မလုပ်ဘဲ ခေါ်ထားလို့ variable ထဲမှာ animal အစား Future ရှိနေပါတယ်။

အောက်က draft မှာ သုံးခုလုံး ပါပါတယ်။ နှစ်ခုက compiler ကို ရပ်စေပါတယ်၊ ဒါပေမယ့် argument လဲနေတာကတော့ မရပ်ပါဘူး၊ ဒါကြောင့် အန္တရာယ်အရှိဆုံးပါ။

registry_draft.dart
class Animal {
  Animal(this.name, this.sound);

  final String name;
  final String? sound;

  int soundLength() {
    if (sound != null) {
      return sound.length;               // error: a public field is not promoted
    }
    return 0;
  }
}

class Dog extends Animal {
  Dog(String name) : super('Woof', name); // swapped, and it still compiles
}

Future<Animal?> fetchAnimal(String key) async {
  await Future<void>.delayed(const Duration(milliseconds: 40));
  return key == 'rex' ? Dog('Rex') : null;
}

Future<void> main() async {
  final animal = fetchAnimal('rex');
  print(animal.name);                    // error: animal is a Future
}

ကျရှုံးပုံ သုံးမျိုး

ပထမ error က compiler က သင့်ကို ကာကွယ်ပေးတာပါ: public field ကို subclass က (library တစ်ခုခုထဲမှာတောင်) override လုပ်နိုင်လို့ ဖတ်တာနှစ်ကြိမ်က တန်ဖိုးတူမယ်လို့ အာမမခံနိုင်ပါဘူး၊ ဒါကြောင့် Dart က null check ကို မယုံပါဘူး။ တတိယက type error ပါ: Future<Animal?> ကို Animal? ဖြစ်စေတာက await ပါ။ ဒုတိယက အန္တရာယ်အရှိဆုံးပါ၊ အဓိပ္ပာယ်မှားနေပေမယ့် type တွေက ကိုက်နေလို့ပါ။ Named parameter တွေနဲ့ အဓိပ္ပာယ်မတူရင် type မတူအောင် လုပ်တာတွေက အဲဒီလို အမှားကို compiler ဖမ်းနိုင်စေတဲ့ tool တွေပါ။

Core concept: nullable field၊ inheritance၊ mixin နဲ့ future တွေ အတူအလုပ်လုပ်ပုံ

ဒီ exercise တွေကို အယူအဆ လေးခုက သယ်ထားပါတယ်။ တစ်ခုစီက သေးပါတယ်၊ skill က အဲဒါတွေ ဘယ်လိုကိုက်ညီအောင် တွဲသုံးလဲဆိုတာမှာပါ။

၁။ Nullable field က 'မသိဘူး' လို့ ပြောတာ၊ 'ဗလာ' မဟုတ်ဘူး

Animal က String? sound လို့ ကြေညာပါတယ်။ null ဆိုတာ sound ကို မသိဘူးလို့ ဆိုလိုပြီး ဗလာ string က မကြားရတဲ့ sound ရှိတယ်လို့ ပြောသလို ဖြစ်နေပါလိမ့်မယ်။ ခေါ်တဲ့သူက ?? နဲ့ '...' လို စာသား ပေးပြီး ဖတ်ပါတယ်။ ဒီလို field တွေကို final ထားပါ၊ ဒါမှ object က ဆောက်ပြီးနောက်ပိုင်း တစ်သမတ်တည်း ရှိနေပါမယ်။

၂။ Subclass တွေက super နဲ့ default ပေးတယ်

Dog(String name) : super(name, sound: 'Woof') က dog ရဲ့ သတ်မှတ်ထားတဲ့ sound ကို Animal constructor ဆီ ပို့ပါတယ်။ Cat(super.name) လို ဘာမှ မပို့တဲ့ subclass က null ကို အမွေဆက်ခံပါတယ်။ Type တူတဲ့ value တွေအတွက် named parameter သုံးပါ: positional String နှစ်ခုကို လဲမိရင် တိတ်တိတ်လေး compile ဖြစ်နေပေမယ့် named parameter လဲမိရင်တော့ မဖြစ်ပါဘူး။

၃။ Mixin က tree ကို မနက်စေဘဲ behaviour ထည့်တယ်

Mixin ဆိုတာ with နဲ့ ကိုက်ညီတဲ့ class ဘယ်ဟာမဆီ ထည့်လို့ရတဲ့ member အစုအဝေးပါ။ mixin Flyable on Animal က Animal ရဲ့ subclass တွေထဲမှာပဲ ပေါင်းထည့်လို့ရတယ်လို့ ပြောတာမို့ သူ့ method တွေက name ကို သုံးနိုင်ပါတယ်။ Dog နဲ့ Bird နှစ်ခုလုံး Animal ကို extend လုပ်ပေမယ့် Bird ပဲ Flyable ထည့်ပြီး animal is Flyable က အဲဒီစွမ်းရည်ကို runtime မှာ စစ်ပါတယ်။

၄။ Future က နောက်မှ ရောက်မယ့် ရလဒ်ကို ဖော်ပြတယ်

fetchAnimal က Future<Animal?> ပြန်ပါတယ်: အချိန်ယူနိုင်ပြီး အဖြေက ဘာမှမရှိတာလည်း ဖြစ်နိုင်ပါတယ်။ await က async function ထဲမှာ Future ကို ဖွင့်ပေးပါတယ်။ မရှိတဲ့ တိရစ္ဆာန်က ပုံမှန်အဖြေမို့ null ဖြစ်ပြီး ဗလာ key က programmer အမှားမို့ ArgumentError ပစ်ပါတယ်၊ အဲဒါက await မှာ ပေါ်ပြီး try/on နဲ့ ဖမ်းနိုင်ပါတယ်။ Future.wait က lookup အများကြီးကို တစ်ပြိုင်နက် စပြီး ပြီးတဲ့အစီအစဉ် မဟုတ်ဘဲ သင်စာရင်းပေးတဲ့ အစီအစဉ်အတိုင်း ရလဒ်တွေ ပြန်ပေးပါတယ်။

မေးခွန်းသုံးရမယ့် tool
Value မရှိနိုင်ဘူးString? သို့မဟုတ် Animal? လို nullable type
Subclass တစ်ခုက သတ်မှတ်ထားတဲ့ default လိုတယ်super(...) call၊ named parameter နဲ့
မဆက်စပ်တဲ့ class တွေက behaviour မျှသုံးတယ်Mixin၊ with နဲ့ ထည့်
ရလဒ်က နောက်မှ ရောက်မယ်Future<T> နဲ့ await
ရလဒ်အများကြီး အတူ လိုတယ်Future.wait၊ ရလဒ်တွေက ခေါ်တဲ့အစီအစဉ်အတိုင်း
Blueprint၊ badge၊ delivery
  1. Base class က မျှသုံး field တွေကို ကိုင်ထား
  2. Subclass က super နဲ့ default သတ်မှတ်
  3. Mixin က optional စွမ်းရည် ထည့်
  4. Future က animal ဒါမှမဟုတ် ဘာမှမရှိတာ ပို့
  5. await နဲ့ ဖွင့်၊ try/on နဲ့ error ဖမ်း

အလွှာတစ်ခုစီက မေးခွန်းမတူတာကို ဖြေပါတယ်: တိရစ္ဆာန်အားလုံးမှာ ဘာရှိလဲ၊ ဒီတစ်ကောင်မှာ ထူးခြားတာ ဘာလဲ၊ တချို့ပဲ ဘာလုပ်နိုင်လဲ၊ ဘယ်လောက်ကြာမှ ရောက်မလဲ။

အခြေခံ ပုံစံ

dart
class Base { Base(this.id, {this.note}); final int id; final String? note; }
class Child extends Base { Child(int id) : super(id, note: 'default'); }

mixin Skill on Base {
  String act() => 'item $id acts';
}

class Worker extends Base with Skill { Worker(super.id); }

Future<Base?> load(int id) async { /* await something, return a Base or null */ }

Nullable field ပါတဲ့ base class၊ super နဲ့ default ပေးတဲ့ subclass၊ base ကို on နဲ့ ကန့်သတ်ထားတဲ့ mixin၊ with နဲ့ ပေါင်းထည့်တဲ့ class နဲ့ nullable ရလဒ်ပြန်တဲ့ async function တစ်ခု။

Reference solution: Animal၊ Dog၊ Cat၊ Bird နဲ့ async lookup

ဒီ solution တွေက task လေးခုလုံးကို ဖုံးပြီး ပထမ draft တွေ လွတ်သွားတတ်တဲ့ case တွေကိုလည်း ထပ်ထည့်ထားပါတယ်: sound မပါတဲ့ တိရစ္ဆာန် (Cat)၊ ဘာမှ မတွေ့တဲ့ lookup ('ghost')၊ ကျရှုံးတဲ့ lookup (ဗလာ key) နဲ့ lookup အများကြီး တစ်ပြိုင်နက်။ fetchAnimal ထဲက 40 ms delay က network call ကို ကိုယ်စားပြုပါတယ်။ await တိုင်းနဲ့ print တိုင်းက main ထဲမှာပဲ ရှိလို့ run တိုင်း output တူပါတယ်။

registry.dart
class Animal {
  Animal(this.name, {this.sound});

  final String name;
  final String? sound; // null means this animal makes no known sound

  String makeSound() => '$name says ${sound ?? '...'}';
}

class Dog extends Animal {
  Dog(String name) : super(name, sound: 'Woof');
}

class Cat extends Animal {
  Cat(super.name); // no sound given, so it stays null
}

mixin Flyable on Animal {
  String fly() => '$name takes off';
}

class Bird extends Animal with Flyable {
  Bird(String name) : super(name, sound: 'Tweet');
}

final _zoo = <String, Animal>{
  'rex': Dog('Rex'),
  'tom': Cat('Tom'),
  'sky': Bird('Sky'),
};

/// Pretends to call a server. Unknown keys give null; an empty key is a bug.
Future<Animal?> fetchAnimal(String key) async {
  await Future<void>.delayed(const Duration(milliseconds: 40));
  if (key.isEmpty) throw ArgumentError('key must not be empty');
  return _zoo[key.toLowerCase()];
}

Future<void> main() async {
  print(Dog('Buddy').makeSound());
  print(Cat('Tom').makeSound());

  final bird = Bird('Sky');
  print(bird.makeSound());
  print(bird.fly());

  for (final key in ['Rex', 'SKY', 'ghost']) {
    final animal = await fetchAnimal(key);
    if (animal == null) {
      print('$key: no such animal');
    } else if (animal is Flyable) {
      print('$key: ${animal.makeSound()} / ${animal.fly()}');
    } else {
      print('$key: ${animal.makeSound()}');
    }
  }

  try {
    await fetchAnimal('');
  } on ArgumentError catch (e) {
    print('caught: ${e.message}');
  }

  final animals = await Future.wait([
    fetchAnimal('tom'),
    fetchAnimal('nope'),
    fetchAnimal('rex'),
  ]);
  print(animals.map((a) => a?.name ?? 'missing').toList());
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Nullable field, named parameterAnimal(this.name, {this.sound});

    this.name နဲ့ this.sound က argument တွေကို final field ထဲ ကူးထည့်ပါတယ်။ Brace က sound ကို named optional parameter ဖြစ်စေလို့ ခေါ်တဲ့သူက sound: 'Woof' လို့ ရေးပြီး call site မှာ value ရဲ့ အဓိပ္ပာယ်ကို မြင်ရပါတယ်။ မထည့်ရင် null သိမ်းပါတယ်။

  2. A fallback for nullString makeSound() => '$name says ${sound ?? '...'}';

    sound က null ဖြစ်ရင် ?? က '...' ကို ရွေးပါတယ်။ Method က print မလုပ်ဘဲ String ပြန်ပေးလို့ ရလဒ်ကို ဘာလုပ်မလဲ main က ဆုံးဖြတ်ပါတယ်။

  3. Defaults through superDog(String name) : super(name, sound: 'Woof');

    Dog က နာမည်ကိုပဲ လက်ခံပြီး သူ့ sound ကို Animal constructor ဆီ ပို့ပါတယ်။ Cat(super.name) ကတော့ sound ကို ဘာမှ မပို့လို့ null ပဲ ကျန်ပြီး Tom says ... လို့ ထွက်ပါတယ်။

  4. A constrained mixinmixin Flyable on Animal {

    on Animal က Animal ရဲ့ subclass တွေမှာပဲ Flyable ကို သုံးလို့ရတယ်လို့ ကန့်သတ်ပြီး mixin ထဲက fly() က name ကို သုံးခွင့်ရပါတယ်။ Bird က with Flyable နဲ့ ပေါင်းထည့်ပေမယ့် Dog နဲ့ Cat က မထည့်ပါဘူး။

  5. A nullable futureFuture<Animal?> fetchAnimal(String key) async {

    async ကြောင့် function က Future ကို အလိုအလျောက် ပြန်ပါတယ်၊ ဒါကြောင့် ကြေညာတဲ့ type က Future<Animal?> ဖြစ်ပေမယ့် body က Animal? value ကိုပဲ return လုပ်ပါတယ်။ မသိတဲ့ key အတွက် _zoo[key.toLowerCase()] က null ဖြစ်ပြီး အဲဒီ null ကို အဖြေအဖြစ် ဆက်ပို့ပါတယ်။

  6. A type check promotes} else if (animal is Flyable) {

    ဒီ branch ရောက်တဲ့အချိန်မှာ ပထမ branch က null ကို ကိုင်ပြီးသားမို့ animal က null မဟုတ်ဘူးလို့ သိပါတယ်၊ is Flyable က fly() ရှိတဲ့ type အဖြစ် promote လုပ်ပေးပါတယ်။ cast မလိုပါဘူး။ Sky က Bird မို့ ပျံနိုင်ပြီး Rex နဲ့ Tom က Flyable မဟုတ်ပါဘူး။

  7. Several lookups at oncefinal animals = await Future.wait([

    Lookup သုံးခုလုံး ချက်ချင်းစပြီး စုစုပေါင်း 120 ms အစား 40 ms လောက်ပဲ ကြာပါတယ်။ ပြန်လာတဲ့ list က input list ရဲ့ အစီအစဉ်အတိုင်း ဖြစ်ပါတယ်: Tom၊ missing၊ Rex။ 'nope' ရဲ့ null ကို နောက်မှ ?? နဲ့ 'missing' အဖြစ် ပြောင်းထားပါတယ်။

You should see
Buddy says Woof
Tom says ...
Sky says Tweet
Sky takes off
Rex: Rex says Woof
SKY: Sky says Tweet / Sky takes off
ghost: no such animal
caught: key must not be empty
[Tom, missing, Rex]
await fetchAnimal('ghost') လုပ်တဲ့အခါ ဘာဖြစ်လဲ
  1. main က fetchAnimal ကို ခေါ်ပြီး Future ရ
  2. main က await မှာ ရပ်စောင့်၊ တခြားအလုပ် run နိုင်
  3. 40 ms ကြာပြီးမှ lookup က map ကို ရှာ
  4. Future က null နဲ့ ပြီးဆုံး
  5. await က null ပြန်ပေးပြီး main က no such animal print

Key တွေ့

await returns the Animal

ဗလာ key

ArgumentError reappears at the await

Public nullable field ကို ဘာကြောင့် promote မလုပ်တာလဲ

if (sound != null) ပြီးရင် sound က null မဟုတ်တဲ့ String ဖြစ်မယ်လို့ မျှော်လင့်ကြပါတယ်။ Public field အတွက်တော့ compiler က ငြင်းပါတယ်၊ subclass (library တစ်ခုခုထဲက ဖြစ်နိုင်) က override လုပ်ပြီး ဒုတိယအကြိမ် ဖတ်ရင် တခြားတန်ဖိုး ပြန်ပေးနိုင်လို့ပါ။ အောက်က snippet မှာ ယုံကြည်စိတ်ချရတဲ့ နည်းလေးမျိုး ပါပါတယ်: field ကို local variable ထဲ ကူးယူ (local တွေက promote လုပ်လို့ရတယ်)၊ switch pattern သုံး၊ ?. နဲ့ ?? သုံး၊ ဒါမှမဟုတ် field ကို private နဲ့ final လုပ်ပါ၊ library အပြင်ကနေ override လုပ်လို့မရလို့ Dart က promote လုပ်ပေးပါတယ်။ Tag တစ်ခုစီက နည်းလေးမျိုးကနေ ရလဒ်လေးခု print ထုတ်ပြီး အားလုံးက တူညီပါတယ်။

promotion.dart
class Tag {
  Tag(this.text, this._code);

  final String? text;
  final String? _code; // private + final: the compiler can promote it

  int viaLocal() {
    final t = text; // copy into a local, and locals can be promoted
    return t == null ? 0 : t.length;
  }

  int viaPattern() => switch (text) {
        final String t => t.length,
        null => 0,
      };

  int viaOperator() => text?.length ?? 0;

  int viaPrivate() {
    if (_code != null) return _code.length;
    return 0;
  }
}

void main() {
  final withText = Tag('hello', 'ab');
  final empty = Tag(null, null);
  for (final tag in [withText, empty]) {
    print([tag.viaLocal(), tag.viaPattern(), tag.viaOperator(), tag.viaPrivate()]);
  }
}

Future တွေ အတူ run လား၊ တစ်ခုပြီးမှ တစ်ခုလား

Lookup တွေကို တစ်ခုချင်း await လုပ်ရင် အချိန်တွေ ပေါင်းထားတဲ့ ကြာချိန်ယူပါတယ်၊ Future.wait ကတော့ အနှေးဆုံးတစ်ခုလောက်ပဲ ကြာပါတယ်။ အောက်က snippet က job တွေ ပြီးသွားတဲ့ အစီအစဉ်နဲ့ result list ရဲ့ အစီအစဉ်ကို မှတ်သားပါတယ်: fast job က အရင်ပြီးပေမယ့် list ထဲမှာ ဒုတိယဖြစ်လို့ သူ့ရလဒ်က ဒုတိယမှာ ရှိပါတယ်။ Future.wait ထဲက future တစ်ခု ကျရှုံးရင် ပေါင်းထားတဲ့ future ကလည်း အဲဒီ error နဲ့ ကျရှုံးပါတယ်၊ ဒါကြောင့် failure တစ်ခုက batch တစ်ခုလုံးကို နစ်စေသင့်လားဆိုတာ ဆုံးဖြတ်ပါ။ Challenge မှာလိုပဲ job တစ်ခုစီထဲမှာ error ကို ဖမ်းထားရင် တခြားဟာတွေ ဆက်အသက်ရှင်ပါတယ်။

wait_order.dart
final events = <String>[];

Future<String> job(String name, int ms) async {
  await Future<void>.delayed(Duration(milliseconds: ms));
  events.add('finished $name');
  return name;
}

Future<void> main() async {
  final results = await Future.wait([job('slow', 120), job('fast', 10)]);
  events.forEach(print);
  print(results);
}

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

Flutter code တွေမှာ ဒီပုံစံတွေ အများကြီး တွေ့ရပါတယ်။ Model class တွေမှာ optional field တွေ ပါတတ်ပါတယ်၊ server ကလာတဲ့ JSON က တချို့ကို ချန်ထားနိုင်လို့ပါ၊ UI ကလည်း ?? နဲ့ ဖတ်ပြီး placeholder ပြပါတယ်။ Flutter ရဲ့ State class တွေက behaviour ကို mix in လုပ်လေ့ရှိပါတယ်၊ ဥပမာ animation မောင်းဖို့ SingleTickerProviderStateMixin ထည့်တဲ့ State class ဟာ base class တစ်ခုပေါ်မှာ with သုံးတာပါပဲ။ Screen အားလုံးလိုလိုက repository က Future ကို await လုပ်ပြီး စတင်ပါတယ်၊ ဘာမှမတွေ့တဲ့ case နဲ့ call ကျရှုံးတဲ့ case ကို ကိုင်တွယ်ရပါတယ်။

Server ဘက်မှာလည်း ဒီ exercise ပဲ ပေါ်ပါတယ်: id နဲ့ user ကို load လုပ်တဲ့ service ရဲ့ ရလဒ်က Future<User?> ဖြစ်ပြီး 'မတွေ့ဘူး' case က 404 response ဖြစ်လာကာ မမှန်တဲ့ input က error ပစ်ပြီး handler က 400 အဖြစ် ပြောင်းပေးပါတယ်။ 'မရှိတာ' နဲ့ 'ပျက်နေတာ' ကို မတူအောင် ထားတာက အဲဒီ response တွေကို မှန်စေပါတယ်။

အမှားများ

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

Type တူတဲ့ argument တွေ လဲမိတာ

Parameter နှစ်ခုလုံး String ဖြစ်ရင် Dog(String name) : super('Woof', name) က compile ဖြစ်ပြီး dog က သူ့နာမည်ကို ပြောပါတယ်။ sound: 'Woof' လို named parameter သုံးပါ ဒါမှမဟုတ် အဓိပ္ပာယ်တစ်ခုစီကို type သီးသန့် ပေးပါ၊ ဒါမှ compiler က လဲနေတာကို မြင်နိုင်ပါမယ်။

await မေ့တာ

fetchAnimal('rex') ရဲ့ ရလဒ်ကို await မလုပ်ဘဲ makeSound() ခေါ်ရင် ဒီ error ပေါ်ပါတယ်: The method 'makeSound' isn't defined for the type 'Future<Animal?>'. await ကို အရင်လုပ်ပါ။ Future<void> function မှာ ဒီအမှားက ပိုဆိုးပါတယ်: compile ဖြစ်ပြီး အလုပ်က နောက်ကွယ်မှာ run ကာ ပစ်တဲ့ error က သင့် try block ကို ကျော်ထွက်သွားပါတယ်။

Public nullable field ကို စစ်ပြီး ပြန်သုံးတာ

if (sound != null) ပြီး sound.length လို့ ရေးရင် Property 'length' cannot be accessed on 'String?' because it is potentially null ဆိုတဲ့ error ပေါ်ပြီး 'sound' refers to a public property so it couldn't be promoted ဆိုတဲ့ မှတ်ချက်လည်း ပါပါတယ်။ Field ကို local variable ထဲ အရင်ကူးပါ။

await လုပ်ပြီးနောက် null ကို မကိုင်တာ

await fetchAnimal('ghost') ပြီးရင် type က Animal? ပါ။ animal.name လို့ ရေးရင် error ပါ: Property 'name' cannot be accessed on 'Animal?' because it is potentially null. Null branch ကို ကိုင်ပါ၊ ?. သုံးပါ ဒါမှမဟုတ် ?? နဲ့ fallback ပေးပါ။

ကန့်သတ်ထားတဲ့ mixin ကို မှားတဲ့ class မှာ သုံးတာ

mixin Flyable on Animal ကို class Robot with Flyable လို့ သုံးလို့မရပါဘူး၊ compiler က ပြောတာက 'Object' doesn't implement 'Animal' so it can't be used with 'Flyable' ပါ။ Animal ကို အရင် extend လုပ်ပါ၊ ဒါမှမဟုတ် mixin က Animal ရဲ့ member တွေ မလိုရင် on clause ကို ဖယ်ပါ။

Mini challenge

Device inventory: nullable firmware၊ mixin နဲ့ async report

Device inventory သေးသေးလေး တစ်ခု ရေးပါ။ Device မှာ firmware (nullable) ပါပြီး Phone က firmware 1.0 ကို default ပေးပါတယ်၊ Laptop က Rechargeable mixin ကို ရပြီး battery ကို 0 ကနေ 100 ထဲ ထိန်းပါတယ်။ findDevice က နောက်ကျပြီး ပြန်ပေးကာ report က id စာရင်းတစ်ခုကို တစ်ပြိုင်နက် ရှာပြီး line တစ်ကြောင်းစီ ပြန်ပေးပါတယ်။

  • Device မှာ name နဲ့ named optional firmware (String?) ပါပြီး status() က firmware ရှိ/မရှိ စာသားကွဲပြီး ပြန်ရမယ်
  • Phone က နာမည်တစ်ခုပဲ လက်ခံပြီး firmware '1.0' ကို super နဲ့ ပို့ရမယ်
  • Rechargeable က Device ပေါ်မှာ on နဲ့ ကန့်သတ်ထားပြီး battery 50 က စကာ charge() နဲ့ drain() တို့က 0..100 အတွင်း ထိန်းရမယ်
  • findDevice က ဗလာ id အတွက် ArgumentError ပစ်ပြီး မသိတဲ့ id အတွက် null ပြန်ရမယ်
  • report က Future.wait နဲ့ တစ်ပြိုင်နက် ရှာပြီး ဗလာ id တစ်ခုကြောင့် report တစ်ခုလုံး မပျက်စေရဘူး၊ ရလဒ်တွေက ပေးတဲ့ id အစီအစဉ်အတိုင်း ရှိရမယ်
inventory.dart
class Device {
  // TODO: final String name and an optional named final String? firmware
  // TODO: String status() -> "<name> (firmware <v>)" or "<name> (no firmware)"
}

class Phone extends Device {
  // TODO: take only the name and pass firmware '1.0' up with super
}

mixin Rechargeable on Device {
  // TODO: int battery starting at 50
  // TODO: charge(int) and drain(int), both keeping battery within 0..100
}

class Laptop extends Device with Rechargeable {
  // TODO: constructor; status() adds ", battery <n>%" to the inherited text
}

// TODO: a map of known devices: 'p1' -> Phone('Pixel'), 'l1' -> Laptop('ThinkBook')

Future<Device?> findDevice(String id) async {
  // TODO: wait about 30 ms, throw ArgumentError for a blank id,
  //       return the device from the map or null
}

Future<List<String>> report(List<String> ids) async {
  // TODO: look the ids up at the same time with Future.wait. One line per id,
  //       in order: the status, "<id>: not found", or "<blank>: <error message>"
}

Future<void> main() async {
  // TODO: charge a Laptop by 70 and drain it by 10 and print its status,
  //       print a fresh Laptop's status, print each line of
  //       report(['p1', 'l1', 'zz', ' ']) and finally print report([])
}

ရလဒ်: Spare (no firmware), battery 90% နဲ့ Fresh (no firmware), battery 50% ပြီးရင် Pixel (firmware 1.0)၊ ThinkBook (no firmware), battery 50%၊ zz: not found၊ <blank>: id must not be blank နဲ့ နောက်ဆုံး [] တို့ ပေါ်ပါမယ်။

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

Cascade notation (..charge(70)..drain(10)) က object တစ်ခုတည်းပေါ်မှာ method အများကြီး ခေါ်ပေးပါတယ်။ clamp(0, 100) က int ကို အပိုင်းအခြားထဲ ထိန်းပေးပါတယ်။ Report အတွက် id တစ်ခုကို line တစ်ကြောင်းပြန်ပေးပြီး helper ထဲမှာ ArgumentError ကို ဖမ်းတဲ့ function သေးသေးလေးတစ်ခု ရေးပါ၊ ပြီးရင် ids.map(...) ထဲ ထည့်ပြီး ရလဒ်ကို Future.wait ကို ပေးပါ။

အဖြေ ကြည့်ရန်
inventory.dart
class Device {
  Device(this.name, {this.firmware});

  final String name;
  final String? firmware;

  String status() {
    final version = firmware;
    return version == null ? '$name (no firmware)' : '$name (firmware $version)';
  }
}

class Phone extends Device {
  Phone(String name) : super(name, firmware: '1.0');
}

mixin Rechargeable on Device {
  int battery = 50;

  void charge(int amount) => battery = (battery + amount).clamp(0, 100);
  void drain(int amount) => battery = (battery - amount).clamp(0, 100);
}

class Laptop extends Device with Rechargeable {
  Laptop(super.name);

  @override
  String status() => '${super.status()}, battery $battery%';
}

final _inventory = <String, Device>{
  'p1': Phone('Pixel'),
  'l1': Laptop('ThinkBook'),
};

Future<Device?> findDevice(String id) async {
  await Future<void>.delayed(const Duration(milliseconds: 30));
  if (id.trim().isEmpty) throw ArgumentError('id must not be blank');
  return _inventory[id];
}

Future<String> lineFor(String id) async {
  try {
    final device = await findDevice(id);
    return device?.status() ?? '$id: not found';
  } on ArgumentError catch (e) {
    return '<blank>: ${e.message}';
  }
}

Future<List<String>> report(List<String> ids) => Future.wait(ids.map(lineFor));

Future<void> main() async {
  final laptop = Laptop('Spare')
    ..charge(70)
    ..drain(10);
  print(laptop.status());
  print(Laptop('Fresh').status());

  for (final line in await report(['p1', 'l1', 'zz', ' '])) {
    print(line);
  }
  print(await report([]));
}

Quick Quiz 1 / 3

Animal(this.name, this.sound) မှာ String parameter နှစ်ခု ရှိပါတယ်။ Dog(String name) : super('Woof', name) နဲ့ Dog('Rex') ဆိုရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

await မလုပ်ဘဲ fetchAnimal('rex') ရဲ့ static type က ဘာလဲ။

Quick Quiz 3 / 3

job('slow', 120) နဲ့ job('fast', 10) ကို Future.wait ကို အဲဒီအစီအစဉ်အတိုင်း ပေးလိုက်ပါတယ်။ ပြန်လာတဲ့ list ထဲမှာ ဘာတွေ ပါလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Nullable field က 'မသိဘူး' ကို ဖော်ပြပြီး ?? နဲ့ ဖတ်ကာ public field ကို promote မလုပ်နိုင်တဲ့အတွက် local variable ထဲ ကူးသုံးပါ
  • Subclass တွေက super နဲ့ default ပေးပြီး type တူတဲ့ parameter တွေအတွက် named parameter သုံးပါ
  • mixin ... on Base က mixin ကို အသုံးပြုနိုင်တဲ့ class တွေကို ကန့်သတ်ပြီး with က ပေါင်းထည့်ပါတယ်
  • Future<T?> ပြန်ပါ၊ await လုပ်ပါ၊ မရှိတာကို null နဲ့ ပျက်နေတာကို exception နဲ့ ခွဲပါ
  • Future.wait က ရလဒ်တွေကို ပေးတဲ့ အစီအစဉ်အတိုင်း ပြန်ပေးပြီး future တစ်ခု ကျရှုံးရင် ပေါင်းထားတာလည်း ကျရှုံးပါတယ်

Dart language overview — Dart

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

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

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

Dart Exercises: OOP နဲ့ Async | Thuta Learning