ရိုးရိုးလေး ရှင်းပြမယ်
Dart ကို ရိုးရိုးလေး ပြောရရင်
Dart ဆိုတာ Google က ထုတ်လုပ်ထားတဲ့ programming language တစ်ခုပါ။ ဖုန်းထဲက Flutter app တစ်ခုကို သုံးဖူးရင် မျက်နှာပြင်တွေ၊ button တွေ၊ ခလုတ်နှိပ်ရင် ဘာဖြစ်မလဲဆိုတာ ဆုံးဖြတ်တဲ့ code တွေ အများစုက Dart နဲ့ ရေးထားတာပါ။ Dart ကို command-line tool တွေနဲ့ server တွေအတွက်လည်း သုံးကြတာမို့ Flutter ကို ကူညီဖို့ပဲ ရှိတဲ့ ဘာသာစကား မဟုတ်ဘဲ general-purpose ဘာသာစကားတစ်ခုပါ။
Dart ကို code မ run ခင်မှာ ကိုယ့် code ကို ဖတ်ပေးတဲ့ သေချာစိတ်ရှိတဲ့ သူငယ်ချင်းတစ်ယောက်လို မြင်ကြည့်ပါ။ Value တစ်ခုချင်းက ဘာအမျိုးအစားလဲ (စာသား၊ ကိန်းပြည့်၊ ဟုတ်/မဟုတ်) ဆိုတာ ရေးထားလိုက်ရင် ကတိတွေ တည်မတည် Dart က စစ်ပေးပါတယ်။ ဒီ course က အစကနေ စပြီး အဲဒီ သူငယ်ချင်းရဲ့ အကျင့်တွေကို lesson တစ်ခုချင်း သင်ပေးသွားမှာပါ။
Problem → Why → Solution
ပြဿနာ: JavaScript အကျင့်တွေ Dart မှာ အတိုင်း မသုံးရဘူး
ဒီ course ကို ရောက်လာတဲ့ လူတော်တော်များများက JavaScript ဒါမှမဟုတ် Python ကနေ လာကြပြီး တခြား tab တစ်ခုမှာ Flutter tutorial ဖွင့်ထားတတ်ပါတယ်။ အလိုလို ရေးမိတာကို ရေးလိုက်ပါတယ်: function keyword၊ variable အတွက် let၊ output အတွက် console.log၊ line အဆုံးမှာ semicolon မပါဘူး။ Dart က run ဖို့ ငြင်းပြီး error အများကြီးကို တစ်ပြိုင်နက် ပြပါတယ်၊ ဘာသာစကားက ခက်ခဲနေသလို ခံစားရပါတယ်။ ပထမဆုံး ပြတဲ့ error က hello.dart:2:1: Error: Type 'function' not found. ပါ။
ဒါတွေထဲမှာ မလိုအပ်ဘဲ လုပ်ထားတာ မရှိပါဘူး။ Dart က idea တူတူကို သူ့ဘာသာ စာလုံးပေါင်းနဲ့ ရေးတဲ့ တခြားဘာသာစကားတစ်ခုဖြစ်ပြီး program ကို စမယ့်အခါ ပိုများတဲ့ အရာတွေကို စစ်ပါတယ်။ ကိုယ်ထိန်းချုပ်ထားတဲ့ program တစ်ခုနဲ့ ပထမ error တွေကို တမင် မြင်ကြည့်ရတာက Flutter project လမ်းတစ်ဝက်မှာ မမျှော်လင့်ဘဲ တွေ့ရတာထက် စိတ်ပူစရာ အများကြီး နည်းပါတယ်။
// JavaScript habits typed into a .dart file
function main() {
let name = "Aye";
console.log("Hi " + name)
}Dart က ဘာကြောင့် တင်းကျပ်လဲ
Dart က ပထမဆုံး line မ run ခင်မှာ ကိုယ့် code ထဲက name တွေ၊ type တွေ၊ semicolon တွေကို အားလုံး compile လုပ်ပြီး စစ်ပါတယ်။ အမှားတစ်ခုက keyboard ရှေ့မှာ စက္ကန့်အနည်းငယ်ပဲ ကုန်ပြီး user ရှေ့မှာ crash ဖြစ်တာ မဟုတ်တော့ပါဘူး။ အဲဒီအတွက် ပေးရတဲ့ ဈေးကတော့ Dart ရဲ့ ကိုယ်ပိုင် စာလုံးပေါင်းကို သင်ရတာပါ: void main()၊ type ပါတဲ့ parameter တွေ၊ semicolon နဲ့ ဆုံးတဲ့ statement တွေပါ။
အဓိက သဘောတရား: ဘာသာစကား၊ toolchain နဲ့ ကတိတွေ
Dart ဆိုတာ အတူတကွ အလုပ်လုပ်တဲ့ အရာသုံးခုပါ။ ပထမတစ်ခုက ဘာသာစကားပါ: statically typed ဖြစ်လို့ compiler က value တိုင်းရဲ့ type ကို သိပါတယ်၊ null-safe ဖြစ်လို့ null ကို ကိုင်နိုင်တဲ့ variable ဆိုရင် သူ့ type မှာ ပြောထားရပါတယ်၊ object-oriented ဖြစ်လို့ ကိန်းဂဏန်း 5 ကအစ အားလုံးနီးပါးက method ပါတဲ့ object တွေပါ။ ဒုတိယတစ်ခုက compiler နဲ့ runtime မိသားစုပါ။ Develop လုပ်နေတုန်းမှာ Dart VM က just-in-time (JIT) compilation နဲ့ code ကို run ပေးပြီး အဲဒါကြောင့် မြန်မြန် restart လုပ်လို့ရတာ၊ Flutter ရဲ့ hot reload ဖြစ်နိုင်တာပါ။ Release အတွက်ကတော့ Dart က ahead-of-time (AOT) နဲ့ native machine code အဖြစ် compile လုပ်နိုင်သလို web အတွက် JavaScript ဒါမှမဟုတ် WebAssembly အဖြစ် ဘာသာပြန်လို့လည်း ရပါတယ်။ တတိယတစ်ခုက toolchain ပါ: code ကို run၊ analyze၊ format၊ test နဲ့ package လုပ်ပေးတဲ့ dart command တစ်ခုတည်းပါ။
| အပိုင်း | ဘာတွေ ပေးလဲ |
|---|---|
| ဘာသာစကား | Static type၊ sound null safety၊ class၊ async/await နဲ့ Dart 3 ရဲ့ record နဲ့ pattern တွေ |
| Compiler နဲ့ runtime | Develop အတွက် JIT ပါတဲ့ VM၊ release အတွက် AOT native code၊ browser အတွက် JavaScript နဲ့ WebAssembly |
| dart command | run, analyze, format, test, compile နဲ့ package တွေအတွက် pub တို့ကို tool တစ်ခုတည်းကနေ |
အဲဒါအားလုံးရဲ့ နောက်မှာ နေ့စဉ်သုံး လုပ်ငန်းစဉ်ရိုးရိုးလေး တစ်ခုပဲ ရှိပါတယ်။ .dart file တစ်ခု ရေးတယ်၊ Dart က စစ်တယ်၊ စစ်တာ ကျော်မှသာ main() စ run ပါတယ်။ ဒီအစီအစဉ်က ဒီ lesson က ယူသွားသင့်ဆုံး အကျင့်ပါ: တစ်ခုခု မှားနေရင် မြင်ရတဲ့ message က စစ်ဆေးတဲ့ အဆင့်ကနေ လာတာဖြစ်ပြီး ဘာမှ မ run ရသေးပါဘူး။
၁။ ရေးပါ
Code ကို .dart နဲ့ ဆုံးတဲ့ file ထဲ သိမ်းပါ။ Run လို့ရတဲ့ file တိုင်းမှာ main() function လိုပါတယ်၊ Dart က အဲဒီကနေ စတာမို့ပါ။
၂။ စစ်ပါတယ်
dart run က file တစ်ခုလုံးကို အရင်ဖတ်ပါတယ်။ စာလုံးပေါင်းမှား၊ semicolon ကျန်၊ type မှားတဲ့ value တွေကို file နာမည်၊ line နဲ့ column ပါ ပြောပြပါတယ်။
၃။ Run ပါတယ်
စစ်တာ ကျော်ရင် main() က ပထမ statement ကနေ နောက်ဆုံး statement အထိ အလုပ်လုပ်ပြီး print() က terminal ထဲ စာသား ရေးပေးပါတယ်။
ဒီ course က Dart 3 ကို သင်ပါတယ်
Dart 3 မှာ sound null safety က အမြဲ ဖွင့်ထားပြီးသားမို့ null-unsafe mode ဆိုတာ မရှိတော့ပါဘူး။ Record၊ pattern နဲ့ class modifier တွေလည်း ဘာသာစကားရဲ့ အစိတ်အပိုင်းတွေ ဖြစ်ပါတယ်။ Release number တိတိကျကျကို ဒီစာမျက်နှာတွေမှာ မပြောပါဘူး၊ official release notes မှာ စစ်ကြည့်ပါ။
- Dart file ရေးမယ်
- Dart က name၊ type နဲ့ ; ကို စစ်မယ်
- main() run ပြီး print က output ပြမယ်
စစ်တာ မအောင်ရင် ဘာမှ မ run ပါဘူး၊ ဒါကြောင့် ပထမဆုံး error message က အကောင်းဆုံး ဆရာပါ။
အထွေထွေ ပုံစံ: Dart file တစ်ခု
ReturnType functionName(Type parameter) {
return value;
}
void main() {
// every statement ends with a semicolon;
functionName(argument);
}Dart file တစ်ခုက function ကြေညာချက်တွေရဲ့ စာရင်းပါ။ Function တစ်ခုစီက return type ကို အရင်ရေးပြီး နာမည်နဲ့ type ပါတဲ့ parameter တွေ ရေးပါတယ်။ Program က main() ကနေ စတာမို့ run လို့ရတဲ့ program တိုင်းမှာ main() ပါပါတယ်။ void ဆိုတာ main() က ဘာ value မှ ပြန်မပေးဘူးလို့ ဆိုလိုပါတယ်။
ပထမဆုံး Dart program လေး
Run မကြည့်ခင် ဒီ program ကို အရင်ဖတ်ပါ။ နှုတ်ဆက်စာသား တည်ဆောက်ပေးတဲ့ function တစ်ခု ကြေညာထားပြီး main() က အဲဒါကို အပိုင်းနှစ်ပိုင်း ခေါ်ပါတယ်။ ပုံနှိပ်မယ့် စာကြောင်းနှစ်ကြောင်းကို ကြိုမှန်းကြည့်ပါ၊ ပြီးရင် နောက် lesson (Get Started) မှာ သင်မယ့် dart run welcome.dart နဲ့ run ပါ၊ ဒါမှမဟုတ် DartPad ထဲ paste လုပ်ကြည့်ပါ။
// welcome.dart: your first Dart program
String welcome(String name, int lessonsLeft) {
return 'Hi $name, $lessonsLeft lessons to go!';
}
void main() {
const total = 29;
var done = 1;
print(welcome('Aye Aye', total - done));
print('Course progress: ${done * 100 ~/ total}%');
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A typed function
String welcome(String name, int lessonsLeft) {ကတိသုံးခုကို line တစ်ကြောင်းတည်းမှာ ပေးထားပါတယ်: name က String၊ lessonsLeft က int၊ function က String တစ်ခု ပြန်ပေးမယ်။ welcome(1, 'Aye') လို နေရာလွှဲပြီး ခေါ်ရင် Dart က ငြင်းပါမယ်။
- String interpolation
return 'Hi $name, $lessonsLeft lessons to go!';String ထဲက $name နဲ့ $lessonsLeft ကို variable ရဲ့ တန်ဖိုးနဲ့ အစားထိုးပေးပါတယ်။ ကိန်းဂဏန်းကို စာသားနဲ့ ကိုယ်တိုင် ပေါင်းစရာ မလိုပါဘူး။
- A constant
const total = 29;const က compile လုပ်နေတုန်းကတည်းက တန်ဖိုး သိပြီးသား အပြောင်းအလဲမရှိတဲ့ value ပါ။ Type ကို မရေးထားပေမယ့် 29 ကနေ int လို့ Dart က ကိုယ်တိုင် သိပါတယ်။
- A variable
var done = 1;var နဲ့ ကြေညာထားတာမို့ done ကို နောက်မှ ပြောင်းလို့ရပါတယ်၊ ဒါပေမယ့် type က int အဖြစ်ပဲ ကျန်ပါမယ်။ Lesson ၅ မှာ var, final, const ကို အသေးစိတ် ကြည့်ပါမယ်။
- An expression as an argument
print(welcome('Aye Aye', total - done));total - done ကို function မခေါ်ခင် တွက်ပြီး 28 ရပါတယ်။ အဲဒီ int က lessonsLeft နေရာ ကိုက်ပါတယ်။ ပြီးတော့ welcome() ပြန်ပေးတဲ့ String ကို print() က ရေးပေးပါတယ်။
- Integer division
print('Course progress: ${done * 100 ~/ total}%');${ } ထဲမှာ expression အပြည့်အစုံ ရေးလို့ရပါတယ်။ ~/ က ကိန်းပြည့်စားခြင်း (integer division) ပါ၊ 100 ~/ 29 က 3 ပါ။ သာမန် / ကို သုံးရင် 3.44... လို double ရမှာပါ။
Hi Aye Aye, 28 lessons to go!
Course progress: 3%- welcome.dart ကို ရေးတယ်
- dart run က file ကို ဖတ်ပြီး စစ်တယ်
- main() စ အလုပ်လုပ်တယ်
- print က line တစ်ကြောင်းချင်း terminal ထဲ ရေးတယ်
စစ်တာ အောင်တယ်
Both lines are printed, then the program ends
စစ်တာ မအောင်ဘူး
No program runs; the errors list file, line and column
ဒီ course ကို ဘယ်လို ခွဲထားလဲ
Course မှာ lesson ၂၉ ခုကို အခန်းငါးခန်း ခွဲထားပြီး အခန်းတစ်ခုချင်းက အရင်အခန်းပေါ်မှာ ဆက်ဆောက်ပါတယ်။ အစကနေ အစီအစဉ်အတိုင်း သွားဖို့ အကြံပြုပါတယ်: async lesson တွေက function, collection နဲ့ null safety ကို သိပြီးသား ဆိုတာကို အခြေခံထားပါတယ်။
| အခန်း | Lesson | သင်ယူရမယ့်အရာ |
|---|---|---|
| Basic | ၁၀ ခု | Dart ဆိုတာဘာလဲ၊ setup၊ syntax၊ variable၊ data type၊ function၊ operator၊ List နဲ့ Map |
| Intermediate | ၇ ခု | if/else၊ loop၊ null safety၊ class၊ constructor၊ inheritance၊ mixin |
| Advanced | ၈ ခု | Record နဲ့ pattern၊ generic၊ sealed type၊ Future နဲ့ Stream၊ isolate၊ package၊ testing |
| Projects | ၂ ခု | Mini project တစ်ခုနဲ့ ဆက်လေ့လာရမယ့် လမ်းညွှန် |
| Exercises | ၂ ခု | Fundamentals နဲ့ OOP/async လေ့ကျင့်ခန်းတွေ |
Lesson တစ်ခုကို ဘယ်လို သုံးမလဲ
Lesson တိုင်းက အစီအစဉ်တူပါတယ်: ရိုးရိုးရှင်းပြချက်၊ ပြဿနာတစ်ခု၊ အဓိက သဘောတရား၊ ယေဘုယျ ပုံစံ၊ line တစ်ကြောင်းချင်း ရှင်းပြထားတဲ့ ဥပမာ၊ အမှားတွေ၊ mini challenge နဲ့ quiz သုံးခုပါ။ အလုပ်ဖြစ်တဲ့ လုပ်ရိုးလုပ်စဉ်ကတော့ ဒီလိုပါ။
၁။ ကြိုမှန်းပါ
Example ကို မ run ခင် ထွက်လာမယ့် output ကို ကိုယ့်စာအုပ်ထဲ ရေးထားပါ။
၂။ Run ပါ
DartPad ဒါမှမဟုတ် dart run နဲ့ run ပြီး ကိုယ့်ခန့်မှန်းချက်နဲ့ နှိုင်းယှဉ်ပါ။
၃။ တမင် ဖျက်ကြည့်ပါ
Semicolon တစ်ခု ဖြုတ်ကြည့်ပါ၊ ဒါမှမဟုတ် type တစ်ခု ပြောင်းကြည့်ပါ။ Error message ကို ဖတ်ပါ။
၄။ Challenge လုပ်ပါ
Starter code ထဲက TODO တွေကို ကိုယ်တိုင် ဖြည့်ပြီးမှ solution ကို ကြည့်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ Dart
လူအများစုက Dart ကို Flutter ကနေတစ်ဆင့် တွေ့ကြပါတယ်: Dart codebase တစ်ခုတည်းက Android၊ iOS၊ web နဲ့ desktop app တွေကို ထုတ်ပေးနိုင်ပြီး အဲဒီ app တွေထဲက widget တိုင်း၊ tap handler တိုင်း၊ network call တိုင်းက Dart code ပါ။ Flutter app တွေက Dart သက်သက်နဲ့ framework တစ်ခု ပေါင်းထားတာမို့ ဒီ course ထဲက ကျွမ်းကျင်မှုတွေ တိုက်ရိုက် သုံးလို့ရပါတယ်။ Null-safety error ဒါမှမဟုတ် async function ကို မဖတ်တတ်တဲ့ Flutter developer ဟာ file တိုင်းမှာ နှေးနေပါလိမ့်မယ်။
Dart ကို Flutter အပြင်မှာလည်း သုံးပါတယ်။ Command-line tool တွေနဲ့ server သေးသေးလေးတွေကို Dart နဲ့ ရေးကြပြီး dart compile command က program တစ်ခုကို သီးခြားရပ်တည်နိုင်တဲ့ executable အဖြစ် ပြောင်းပေးနိုင်ပါတယ်။ Library တွေကို pub.dev package site ကနေ မျှဝေကြပြီး team တွေက merge မလုပ်ခင် dart format နဲ့ dart analyze ကို အလိုအလျောက် run ပြီး code ကို သပ်ရပ်အောင် ထိန်းကြပါတယ်။
အမှားများ
အစပိုင်းမှာ မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ
၁။ JavaScript ဒါမှမဟုတ် Python ကို မှတ်ဥာဏ်နဲ့ ရိုက်တာ
function၊ let၊ console.log ဒါမှမဟုတ် semicolon မပါတဲ့ အကျင့်တွေက error အရှည်ကြီး ထွက်စေပါတယ်။ ပထမ error ကိုပဲ ဖတ်ပြီး ပြင်ကာ ပြန် run ပါ၊ နောက်ပိုင်း error တွေက ပထမ error ရဲ့ အကျိုးဆက်တွေ ဖြစ်တတ်ပါတယ်။
၂။ Plain Dart မသိခင် Flutter ကို စတာ
Flutter error message တစ်ခုထဲမှာ Dart ဘာသာစကား error တွေနဲ့ framework error တွေ ရောနေလို့ ဘယ်ဟာက ဘာလဲ ခွဲရခက်ပါတယ်။ Plain Dart file တွေကို အရင် run ပါ၊ ဒါမှ widget ထဲမှာ type error ဒါမှမဟုတ် null error ပေါ်တဲ့အခါ ရင်းနှီးပြီးသား ဖြစ်နေပါမယ်။
၃။ Output ကို မခန့်မှန်းဘဲ ဥပမာကို ဖတ်ရုံ run တာ
Example ကို copy ကူးပြီး run ရုံနဲ့ copy ကူးတတ်တာပဲ သက်သေပြတာပါ။ Run မလုပ်ခင် ထွက်မယ့် line တွေကို ရေးမှတ်ပြီး နောက်မှ နှိုင်းယှဉ်ပါ။ ခန့်မှန်းချက်နဲ့ တကယ့် output ကွာတဲ့ နေရာမှာပဲ နားမလည်မှု ပုန်းနေတာပါ။
Mini challenge
Mini challenge: progress line
ပြီးသွားတဲ့ lesson အရေအတွက်နဲ့ စုစုပေါင်းကို လက်ခံပြီး 6/29 lessons done (20%) လိုမျိုး string ပြန်ပေးတဲ့ typed function progressLine ကို ရေးပါ။ ပြီးရင် main() ကနေ ခေါ်ပြီး ရလဒ်သုံးခုကို print ထုတ်ပါ။
- Parameter နှစ်ခုလုံးကို int၊ return type ကို String လို့ ရေးပါ
- ရာခိုင်နှုန်းကို ကိန်းပြည့်စားခြင်း ~/ နဲ့ တွက်ပါ
- total က 0 ဖြစ်ရင် 0 နဲ့ မစားဘဲ 0% လို့ ပြပါ
- main() မှာ (6, 29)၊ (29, 29) နဲ့ (0, 0) အတွက် print ထုတ်ပါ၊ မ run ခင် output ကို ကြိုမှန်းပါ
// TODO: give both parameters the type int and the function a String return type
progressLine(done, total) {
// TODO: if total is 0 the percentage is 0 (never divide by zero)
// TODO: otherwise use integer division on done * 100
// TODO: return text such as '6/29 lessons done (20%)'
}
void main() {
// TODO: print the progress line for 6 of 29, 29 of 29 and 0 of 0
}ရလဒ်: Program က အောက်ပါ line သုံးကြောင်းကို print ထုတ်ပါမယ်: 6/29 lessons done (20%) 29/29 lessons done (100%) 0/0 lessons done (0%)
အရိပ်အမြွက် ကြည့်ရန်
ပထမ percent ကို တွက်ပါ: total == 0 ? 0 : done * 100 ~/ total။ ပြီးမှ string interpolation နဲ့ '$done/$total lessons done ($percent%)' လို့ ဆက်ပါ။ done * 100 ကို အရင်မြှောက်တာက ~/ မစားခင် ရလဒ်ကို မကျဆင်းစေဖို့ပါ။
အဖြေ ကြည့်ရန်
String progressLine(int done, int total) {
final percent = total == 0 ? 0 : done * 100 ~/ total;
return '$done/$total lessons done ($percent%)';
}
void main() {
print(progressLine(6, 29));
print(progressLine(29, 29));
print(progressLine(0, 0));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Dart က Google ထုတ်တဲ့ typed, null-safe ဘာသာစကားဖြစ်ပြီး Flutter၊ command-line tool နဲ့ server တွေမှာ သုံးပါတယ်
- Dart ကို ရေး၊ စစ်၊ run ဆိုတဲ့ အစီအစဉ်နဲ့ အလုပ်လုပ်ပါတယ်၊ စစ်တာ မအောင်ရင် ဘာမှ မ run ပါဘူး
- Run လို့ရတဲ့ file တိုင်းမှာ main() ပါရမယ်၊ statement တိုင်းက semicolon နဲ့ ဆုံးပါတယ်
- Course က lesson ၂၉ ခုကို Basic, Intermediate, Advanced, Projects, Exercises အစီအစဉ်နဲ့ သွားပါ
Dart 3.13 changelog — Dart