ရိုးရိုးလေး ရှင်းပြမယ်
Setup ကို ရိုးရိုးလေး ပြောရရင်
Dart က ကိုယ့် code ကို run ပေးနိုင်ဖို့ ကိုယ့်ကွန်ပျူတာထဲက တစ်ခုခုက အဲဒီ code ကို ဖတ်ပေးရပါမယ်။ လွယ်တဲ့ နည်းနှစ်ခု ရှိပါတယ်။ DartPad ဆိုတာ Dart ကို ရိုက်ပြီး Run နှိပ်ရုံပဲ ရတဲ့ web page ပါ၊ install လုပ်စရာ မလိုပါဘူး။ Dart SDK ကတော့ download တစ်ခုဖြစ်ပြီး dart ဆိုတဲ့ command တစ်ခုကို ကွန်ပျူတာထဲ ထည့်ပေးပါတယ်၊ အဲဒီ command က file တွေကို run၊ စစ်၊ format လုပ်ပေးပြီး project အသစ်တွေကိုလည်း ဆောက်ပေးပါတယ်။
နောက်ဆက်တွဲ တစ်မိနစ်အတွင်း example တစ်ခုကို စမ်းကြည့်ချင်ရင် DartPad နဲ့ စပါ၊ file တွေ သိမ်းချင်လာတာ၊ project ဆောက်ချင်လာတာ ဒါမှမဟုတ် Flutter tutorial လိုက်ချင်လာတာနဲ့ SDK ကို install လုပ်ပါ။ နှစ်ခုလုံးက Dart code အတူတူကိုပဲ run ပါတယ်။
Problem → Why → Solution
ပြဿနာ: code မှန်ပေမယ့် run လို့မရ
Lesson ထဲက program ကို အတိအကျ copy ကူးပြီး hello.dart အဖြစ် သိမ်းကာ dart run hello.dart လို့ ရိုက်လိုက်တဲ့အခါ terminal က dart ကို မသိဘူးလို့ ပြောပါတယ်။ Bash မှာ အဲဒါက bash: dart: command not found ဖြစ်ပြီး PowerShell မှာတော့ The term 'dart' is not recognized as the name of a cmdlet, function, script file, or operable program ဖြစ်ပါတယ်။ အဲဒါကို ဖြေရှင်းပြီးတဲ့အခါ နောက်တစ်ခါ file ကို ရှာမတွေ့ဘူးဆိုတဲ့ error ပေါ်လာပါတယ်။
အဲဒီနှစ်ခုလုံးကို ဖတ်ရင် Dart code မှားနေတယ်လို့ ထင်ရပေမယ့် အတိအကျ ဖြစ်တာက code ရဲ့ ပြဿနာ မဟုတ်ဘဲ ဘယ်နေရာမှာ ဘာရှိလဲဆိုတဲ့ ပြဿနာတွေပါ။
$ dart run hello.dart
bash: dart: command not found
# later, after installing the SDK, but in the wrong folder:
$ dart run hello.dart
Could not find file `hello.dart`ဘာကြောင့် ဒီ error တွေ ဖြစ်လဲ
ပထမ message က PATH ထဲ ဖော်ပြထားတဲ့ folder တွေအားလုံးမှာ shell က ရှာလို့ dart ဆိုတဲ့ program မတွေ့ဘူးလို့ ပြောတာပါ: SDK မ install ရသေးတာ၊ ဒါမှမဟုတ် bin folder က PATH ထဲ မရှိတာ၊ ဒါမှမဟုတ် PATH ပြောင်းမယ့်အချိန်မတိုင်ခင် terminal ကို ဖွင့်ထားတာ ဖြစ်ပါတယ်။ ဒုတိယ message က shell က dart ကို တွေ့ပေမယ့် လက်ရှိ folder ထဲမှာ အဲဒီ file မရှိဘူးလို့ ပြောတာပါ။
အဓိက သဘောတရား: tool ကို ရှာ၊ ပြီးမှ file ကို ရှာ
အရင်ဆုံး ကိုယ့်အခြေအနေနဲ့ ကိုက်တဲ့ နည်းကို ရွေးပါ။ အောက်က table မှာ တစ်ခုချင်းစီ ဘာအတွက် အကောင်းဆုံးလဲ ပြထားပါတယ်။ ဒီ course ထဲက ဥပမာတိုင်းက main() ပါတဲ့ file တစ်ခုတည်းဖြစ်လို့ နည်းလေးမျိုးလုံး အလုပ်လုပ်ပါတယ်။
| နည်း | ဘာလိုလဲ | အကောင်းဆုံး အသုံးဝင်ပုံ |
|---|---|---|
| DartPad | Browser ပဲ (dartpad.dev) | Example တွေကို မြန်မြန် စမ်းကြည့်ဖို့ |
| Dart SDK + terminal | SDK download လုပ်ပြီး terminal သုံးရန် | File တွေ သိမ်းဖို့၊ project ဆောက်ဖို့၊ CI နဲ့ script |
| Editor + Dart plugin | VS Code ရဲ့ Dart extension ဒါမှမဟုတ် IntelliJ / Android Studio ရဲ့ Dart plugin | Autocomplete၊ error ကို ရေးနေတုန်း မြင်ဖို့ |
| Flutter SDK | Flutter ကို install လုပ်တာနဲ့ dart command ပါပါတယ် | Flutter app တွေ တည်ဆောက်ဖို့ ရည်မှန်းရင် |
Command-line နည်းကို အဆင့်လိုက် သွားပါမယ်။ အဆင့်တစ်ခုချင်းရဲ့ ရလဒ်ကို စစ်ပြီးမှ နောက်တစ်ဆင့် သွားပါ၊ ပြဿနာတက်ရင် ဘယ်အဆင့်က ဖြစ်တာလဲ ချက်ချင်း သိပါလိမ့်မယ်။
၁။ SDK ကို install လုပ်ပါ
ကိုယ့် operating system အတွက် dart.dev က install စာမျက်နှာကို လိုက်လုပ်ပါ။ macOS မှာ Homebrew၊ Windows မှာ Chocolatey၊ Linux မှာ apt ဆိုတဲ့ package manager တွေ ရှိသလို zip download နဲ့ အဲဒီထဲက bin folder ကို PATH ထဲ ထည့်တာလည်း ရပါတယ်။
၂။ Terminal အသစ်မှာ စစ်ပါ
Terminal အသစ်ဖွင့်ပြီး dart --version ကို run ပါ။ Dart SDK version: နဲ့ စပြီး version နံပါတ်ပါတဲ့ စာတစ်ကြောင်း ပေါ်ရပါမယ်။ PATH ပြောင်းမယ့်အချိန်မတိုင်ခင် ဖွင့်ထားတဲ့ terminal တွေက PATH အဟောင်းကိုပဲ ဆက်သုံးပါတယ်။
၃။ File တစ်ခုကို run ပါ
hello.dart ကို သိမ်းပြီး cd နဲ့ အဲဒီ file ရှိတဲ့ folder ထဲ ဝင်ကာ dart run hello.dart လို့ ရိုက်ပါ။ File နာမည်က ပေးထားတဲ့အတိုင်း အတိအကျ ကိုက်ရပါမယ်။
၄။ File တစ်ခုထက်ပိုရင် project ဆောက်ပါ
dart create hello_app နဲ့ project folder ဆောက်ပြီး cd hello_app နဲ့ ဝင်ကာ dart run လို့ ရိုက်ပါ။ Project ထဲမှာ dart run က file နာမည် မလိုဘဲ bin/ ထဲက entry point ကို ရှာပြီး run ပေးပါတယ်။
File တစ်ခုတည်းဆိုရင် main() ပါရုံနဲ့ လုံလောက်ပါတယ်။ Project ကတော့ package ရဲ့ နာမည်၊ SDK ကန့်သတ်ချက်၊ dependency တွေ ပါတဲ့ pubspec.yaml၊ entry point တွေအတွက် bin/၊ ပြန်သုံးလို့ရတဲ့ code အတွက် lib/၊ test တွေအတွက် test/၊ lint စည်းမျဉ်းတွေအတွက် analysis_options.yaml တို့ကို ထပ်ထည့်ပေးပါတယ်။
- Terminal က PATH မှာ dart ကို ရှာမယ်
- dart က ကိုယ့် file ဒါမှမဟုတ် project ကို ရှာမယ်
- VM က main() run ပြီး output ပြမယ်
ပထမနေ့ error အများစုက ဒီ ရှာဖွေမှုနှစ်ခုထဲက တစ်ခု မအောင်တာပါ။ Code ကို မထိခင် ဘယ်တစ်ခု မအောင်လဲ အရင်မေးပါ။
အထွေထွေ ပုံစံ: command-line workflow
dart --version
dart run file_name.dart
dart create project_name
cd project_name
dart runပထမ line က SDK က PATH ပေါ်မှာ ရှိတာကို သက်သေပြပါတယ်။ dart run file_name.dart က file တစ်ခုကို run ပါတယ်။ dart create က project folder ဆောက်ပေးပြီး အဲဒီ folder ထဲမှာ dart run က bin/ ထဲက entry point ကနေ စပါတယ်။ File နာမည်၊ project နာမည်ကို ကြိုက်သလို ပေးလို့ရပါတယ်။
Setup စစ်ဖို့ ပထမဆုံး program
ဒီ program ကို hello.dart ထဲ သိမ်းပြီး အပေါ်က command ကို run ပါ။ DartPad မှာဆိုရင် paste လုပ်ပြီး Run ကို နှိပ်ပါ။ နှစ်ကြောင်း ပေါ်လာရင် SDK ရော ကိုယ့် file ရောက ဖတ်လို့ရပြီဆိုတဲ့ သက်သေပါ။ ဒုတိယ line က တွက်ချက်မှု ပါတာမို့ run တာ တကယ်ဖြစ်တာကို ပိုသေချာစေပါတယ်။
// hello.dart: save this file, then run: dart run hello.dart
void main() {
print('Dart setup is ready.');
print('2 + 3 = ${2 + 3}');
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A comment with the command
// hello.dart: save this file, then run: dart run hello.dart// နဲ့ စတဲ့ line က comment ပါ၊ Dart က ကျော်သွားပါတယ်။ File နာမည်နဲ့ run ရမယ့် command ကို ကိုယ့်ကိုယ်ကို သတိပေးဖို့ ရေးထားတာပါ။
- The entry point
void main() {dart run က ဒီ function ကနေ စပါတယ်။ File ထဲမှာ main() မရှိရင် run ဖို့ ဘာမှ မရှိတော့လို့ Dart က ငြင်းပါတယ်။
- Printing text
print('Dart setup is ready.');print() က စာသားကို terminal ထဲ ရေးပေးပြီး line အသစ်ကို ဆင်းပါတယ်။ ဒီ line ပေါ်လာရင် SDK အလုပ်လုပ်နေပြီပါ။
- A calculation inside a string
print('2 + 3 = ${2 + 3}');${ } ထဲက 2 + 3 ကို အရင်တွက်ပြီး ရလဒ် 5 ကို စာသားထဲ ထည့်ပေးပါတယ်။ ဒါကြောင့် output က '2 + 3 = 5' ပါ။
Dart setup is ready.
2 + 3 = 5- dart run hello.dart လို့ ရိုက်တယ်
- Shell က PATH မှာ dart ကို ရှာတယ်
- dart က hello.dart ကို ဖတ်ပြီး စစ်တယ်
- main() run ပြီး print က output ရေးတယ်
အဆင့်အားလုံး အောင်တယ်
Two lines appear and the command returns to the prompt
အဆင့်တစ်ခု မအောင်ဘူး
The message names the step: command not found, file not found, or a code error
Dart project တစ်ခုထဲမှာ ဘာတွေ ပါလဲ
dart create hello_app က အောက်က ပုံစံ folder ကို ဆောက်ပေးပါတယ်။ pubspec.yaml က project ရဲ့ မှတ်ပုံတင်ပါ: နာမည်၊ version၊ ဘယ် SDK version တွေနဲ့ အလုပ်လုပ်လဲ၊ ဘယ် package တွေ လိုလဲဆိုတာ ဒီမှာ ရေးပါတယ်။
Layout created by dart create hello_app
---------------------------------------
hello_app/
pubspec.yaml name, SDK range, dependencies
analysis_options.yaml lint rules used by dart analyze
bin/hello_app.dart entry point: dart run starts here
lib/hello_app.dart reusable code that other files import
test/hello_app_test.dart tests run by dart testname: hello_app
description: A sample command-line application.
version: 1.0.0
environment:
sdk: ^3.0.0sdk: ^3.0.0 ဆိုတာက Dart 3.0.0 နဲ့ အဲဒီအထက်၊ Dart 4 မတိုင်ခင်အထိ ဆိုတဲ့ အဓိပ္ပာယ်ပါ။ dart create က ကိုယ်သုံးနေတဲ့ SDK version နဲ့ ကိုက်တဲ့ နံပါတ်ကို ကိုယ်တိုင် ရေးပေးမှာမို့ ကိုယ့် pubspec.yaml ထဲက နံပါတ်က ဒီမှာပြတာနဲ့ မတူနိုင်ပါဘူး။
နေ့စဉ် သုံးမယ့် dart command တွေ
| Command | ဘာလုပ်လဲ |
|---|---|
| dart run | File တစ်ခု ဒါမှမဟုတ် project ရဲ့ entry point ကို run တယ် |
| dart analyze | Run မလုပ်ဘဲ error နဲ့ warning တွေ ရှာတယ် |
| dart format . | Code တွေကို Dart style အတိုင်း စနစ်တကျ ပြန်ညှိတယ် |
| dart test | Project ထဲက test တွေကို run တယ် |
| dart pub get | pubspec.yaml ထဲက package တွေကို ဆွဲယူတယ် |
| dart compile exe | Program ကို သီးခြားရပ်တည်နိုင်တဲ့ executable အဖြစ် ပြောင်းတယ် |
ပထမနေ့ error တွေကို ရှာဖွေဖြေရှင်းခြင်း
| Message | အကြောင်းရင်း | ဖြေရှင်းနည်း |
|---|---|---|
| bash: dart: command not found | SDK မရှိဘူး ဒါမှမဟုတ် bin က PATH ထဲ မပါဘူး | Install လုပ်၊ PATH ထဲ ထည့်ပြီး terminal အသစ် ဖွင့်ပါ |
| Could not find file `hello.dart` | Folder မှား သို့မဟုတ် file နာမည်မှား | cd နဲ့ file ရှိတဲ့ folder ထဲ ဝင်ပြီး ls ဒါမှမဟုတ် dir နဲ့ စစ်ပါ |
| Found no `pubspec.yaml` file | Project အပြင်မှာ file နာမည်မပါဘဲ dart run လုပ်တာ | dart run hello.dart လို့ နာမည်ပေးပါ ဒါမှမဟုတ် project folder ထဲ ဝင်ပါ |
လက်တွေ့ project တွေမှာ
လက်တွေ့ team တွေမှာ setup ကို ဘယ်လို လုပ်လဲ
Team project တွေမှာ ဘယ်သူမှ ကိုယ့်စက်ထဲ SDK ကို ကျပန်း version နဲ့ မထားကြပါဘူး။ pubspec.yaml ထဲက sdk ကန့်သတ်ချက်က project က ဘယ် Dart version တွေ လက်ခံလဲ ပြောပြီး CI server တွေကလည်း dart-lang/setup-dart လို action နဲ့ အဲဒီ version ကို install လုပ်ကာ dart pub get၊ dart analyze နဲ့ dart test ကို အစဉ်လိုက် run ကြပါတယ်။ ဒါကြောင့် developer တိုင်းနဲ့ server က tool အတူတူနဲ့ build ဖြစ်ပါတယ်။
Editor ထဲမှာတော့ VS Code ရဲ့ Dart extension နဲ့ IntelliJ ရဲ့ Dart plugin တို့က analyzer ကို နောက်ကွယ်မှာ run ထားလို့ ဒီ lesson မှာ သင်တဲ့ dart analyze ကို ကိုယ်တိုင် မရိုက်ရဘဲ အနီမျဉ်းတွေကို ရေးနေတုန်း မြင်ရပါတယ်။ ဒါပေမယ့် ဒီ lesson က command တွေကို နားလည်ထားတာက အရေးကြီးပါတယ်: editor ကနေ run လို့ မရတဲ့အခါ terminal မှာ အဲဒီ command ကို ကိုယ်တိုင် ရိုက်ပြီး ပြဿနာက tool ဘက်လား code ဘက်လား ခွဲကြည့်လို့ရပါတယ်။
အမှားများ
Setup မှာ အဖြစ်များတဲ့ အမှားတွေ
၁။ PATH ပြောင်းပြီး terminal အဟောင်းကို ဆက်သုံးတာ
Install လုပ်ပြီးနောက်မှာလည်း command not found ပေါ်နေတာမျိုးပါ။ ဖွင့်ထားပြီးသား terminal က PATH အဟောင်းကို ကိုင်ထားလို့ပါ။ Terminal ကို ပိတ်ပြီး အသစ်ဖွင့်ပါ၊ VS Code ရဲ့ built-in terminal ဆိုရင် VS Code ကိုပါ ပြန်စပါ။
၂။ မှားတဲ့ folder ကနေ run တာ
Terminal က ပုံမှန်အားဖြင့် home folder မှာ စပါတယ်၊ ကိုယ့် file ရှိတဲ့ folder မှာ မဟုတ်ပါဘူး။ Could not find file ပေါ်ရင် cd နဲ့ file ရှိတဲ့ folder ထဲ ဝင်ပြီး ls (Windows မှာ dir) နဲ့ file နာမည်ကို မျက်စိနဲ့ မြင်ရအောင် စစ်ပါ။
၃။ Windows မှာ hello.dart.txt အဖြစ် သိမ်းမိတာ
Windows က file extension ကို ဖျောက်ထားတတ်လို့ Notepad မှာ သိမ်းတဲ့အခါ hello.dart.txt ဖြစ်သွားတတ်ပါတယ်။ File ထဲက code မှန်နေပေမယ့် dart run hello.dart က file မတွေ့ပါဘူး။ Extension ကို ပြစေပြီး နာမည်ကို hello.dart လို့ ပြင်ပါ။
Mini challenge
Mini challenge: setup စစ်ဆေးသူ program
Dart SDK ရဲ့ major version ကို ဖတ်ပြီး ဒီ course အတွက် လိုအပ်တဲ့ Dart 3 ကို ပြည့်မီမပြည့်မီ၊ ဘယ် operating system မှာ run နေလဲ ပြောပေးတဲ့ program ကို ရေးပါ။
- dart:io ကို import လုပ်ပြီး Platform.version ကို သုံးပါ
- majorVersion() function က Platform.version ထဲက ပထမ နံပါတ်ကို int အဖြစ် ပြန်ပေးရမယ်
- main() မှာ major version နဲ့ major >= 3 ရလဒ် (true/false) ကို print ထုတ်ပါ
- Platform.operatingSystem ကိုလည်း print ထုတ်ပါ
import 'dart:io';
int majorVersion() {
// TODO: Platform.version starts with text like '3.4.0 (stable) ...'
// TODO: keep the part before the first space, then the part before the first dot
// TODO: convert it with int.parse and return it
}
void main() {
// TODO: print the major version
// TODO: print whether it is 3 or higher
// TODO: print Platform.operatingSystem
}ရလဒ်: Dart 3 SDK မှာ ပထမ line က Dart major version: 3၊ ဒုတိယ line က Dart 3 or newer: true ဖြစ်ပါမယ်။ တတိယ line က ကိုယ့်စက်ပေါ်မူတည်ပြီး windows၊ linux ဒါမှမဟုတ် macos ပါ။
အရိပ်အမြွက် ကြည့်ရန်
Platform.version.split(' ').first က '3.13.7' လို စာတစ်ခု ပေးပါတယ်။ အဲဒါကို .split('.').first နဲ့ ထပ်ဖြတ်ပြီး int.parse ထဲ ထည့်ပါ။
အဖြေ ကြည့်ရန်
import 'dart:io';
int majorVersion() {
final firstWord = Platform.version.split(' ').first;
return int.parse(firstWord.split('.').first);
}
void main() {
final major = majorVersion();
print('Dart major version: $major');
print('Dart 3 or newer: ${major >= 3}');
print('Operating system: ${Platform.operatingSystem}');
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- DartPad က install မလိုဘဲ စမ်းဖို့၊ Dart SDK က file နဲ့ project တွေအတွက် ဖြစ်ပြီး Flutter SDK မှာလည်း dart command ပါပါတယ်
- dart --version ကို terminal အသစ်မှာ run ပြီး install ကို စစ်ပါ၊ PATH ပြောင်းရင် terminal အသစ် လိုပါတယ်
- dart run hello.dart က file တစ်ခုကို၊ project folder ထဲက dart run က bin/ ထဲက entry point ကို run ပါတယ်
- ပထမနေ့ error အများစုက tool မတွေ့တာ ဒါမှမဟုတ် file မတွေ့တာပါ၊ code ပြဿနာ မဟုတ်ပါဘူး
Dart 3.13 changelog — Dart