Thuta Learning
Dart
BasicProgrammingအခြေခံ

Get Started

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

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

  • လက်ရှိ အလုပ်အတွက် browser ထဲက DartPad နဲ့ local SDK ထဲက ကိုက်ညီတာကို ရွေးနိုင်ဖို့
  • dart --version နဲ့ install မှန်မမှန် စစ်ပြီး PATH ပြဿနာကို မှတ်မိနိုင်ဖို့
  • dart run hello.dart နဲ့ file တစ်ခုကို၊ dart run နဲ့ project တစ်ခုကို run နိုင်ဖို့
  • dart create နဲ့ project ဆောက်ပြီး pubspec.yaml၊ bin/၊ lib/ တို့ ဘာအတွက်လဲ ပြောနိုင်ဖို့

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

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 ရဲ့ ပြဿနာ မဟုတ်ဘဲ ဘယ်နေရာမှာ ဘာရှိလဲဆိုတဲ့ ပြဿနာတွေပါ။

terminal
$ 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 တစ်ခုတည်းဖြစ်လို့ နည်းလေးမျိုးလုံး အလုပ်လုပ်ပါတယ်။

နည်းဘာလိုလဲအကောင်းဆုံး အသုံးဝင်ပုံ
DartPadBrowser ပဲ (dartpad.dev)Example တွေကို မြန်မြန် စမ်းကြည့်ဖို့
Dart SDK + terminalSDK download လုပ်ပြီး terminal သုံးရန်File တွေ သိမ်းဖို့၊ project ဆောက်ဖို့၊ CI နဲ့ script
Editor + Dart pluginVS Code ရဲ့ Dart extension ဒါမှမဟုတ် IntelliJ / Android Studio ရဲ့ Dart pluginAutocomplete၊ error ကို ရေးနေတုန်း မြင်ဖို့
Flutter SDKFlutter ကို 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 တို့ကို ထပ်ထည့်ပေးပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: tool ကို ရှာ၊ ပြီးမှ file ကို ရှာ
  1. Terminal က PATH မှာ dart ကို ရှာမယ်
  2. dart က ကိုယ့် file ဒါမှမဟုတ် project ကို ရှာမယ်
  3. VM က main() run ပြီး output ပြမယ်

ပထမနေ့ error အများစုက ဒီ ရှာဖွေမှုနှစ်ခုထဲက တစ်ခု မအောင်တာပါ။ Code ကို မထိခင် ဘယ်တစ်ခု မအောင်လဲ အရင်မေးပါ။

အထွေထွေ ပုံစံ: command-line workflow

bash
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
// hello.dart: save this file, then run: dart run hello.dart
void main() {
  print('Dart setup is ready.');
  print('2 + 3 = ${2 + 3}');
}

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

  1. A comment with the command// hello.dart: save this file, then run: dart run hello.dart

    // နဲ့ စတဲ့ line က comment ပါ၊ Dart က ကျော်သွားပါတယ်။ File နာမည်နဲ့ run ရမယ့် command ကို ကိုယ့်ကိုယ်ကို သတိပေးဖို့ ရေးထားတာပါ။

  2. The entry pointvoid main() {

    dart run က ဒီ function ကနေ စပါတယ်။ File ထဲမှာ main() မရှိရင် run ဖို့ ဘာမှ မရှိတော့လို့ Dart က ငြင်းပါတယ်။

  3. Printing textprint('Dart setup is ready.');

    print() က စာသားကို terminal ထဲ ရေးပေးပြီး line အသစ်ကို ဆင်းပါတယ်။ ဒီ line ပေါ်လာရင် SDK အလုပ်လုပ်နေပြီပါ။

  4. A calculation inside a stringprint('2 + 3 = ${2 + 3}');

    ${ } ထဲက 2 + 3 ကို အရင်တွက်ပြီး ရလဒ် 5 ကို စာသားထဲ ထည့်ပေးပါတယ်။ ဒါကြောင့် output က '2 + 3 = 5' ပါ။

You should see
Dart setup is ready.
2 + 3 = 5
Terminal ကနေ output အထိ
  1. dart run hello.dart လို့ ရိုက်တယ်
  2. Shell က PATH မှာ dart ကို ရှာတယ်
  3. dart က hello.dart ကို ဖတ်ပြီး စစ်တယ်
  4. 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 တွေ လိုလဲဆိုတာ ဒီမှာ ရေးပါတယ်။

text
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 test
pubspec.yaml
name: hello_app
description: A sample command-line application.
version: 1.0.0

environment:
  sdk: ^3.0.0

sdk: ^3.0.0 ဆိုတာက Dart 3.0.0 နဲ့ အဲဒီအထက်၊ Dart 4 မတိုင်ခင်အထိ ဆိုတဲ့ အဓိပ္ပာယ်ပါ။ dart create က ကိုယ်သုံးနေတဲ့ SDK version နဲ့ ကိုက်တဲ့ နံပါတ်ကို ကိုယ်တိုင် ရေးပေးမှာမို့ ကိုယ့် pubspec.yaml ထဲက နံပါတ်က ဒီမှာပြတာနဲ့ မတူနိုင်ပါဘူး။

နေ့စဉ် သုံးမယ့် dart command တွေ

Commandဘာလုပ်လဲ
dart runFile တစ်ခု ဒါမှမဟုတ် project ရဲ့ entry point ကို run တယ်
dart analyzeRun မလုပ်ဘဲ error နဲ့ warning တွေ ရှာတယ်
dart format .Code တွေကို Dart style အတိုင်း စနစ်တကျ ပြန်ညှိတယ်
dart testProject ထဲက test တွေကို run တယ်
dart pub getpubspec.yaml ထဲက package တွေကို ဆွဲယူတယ်
dart compile exeProgram ကို သီးခြားရပ်တည်နိုင်တဲ့ executable အဖြစ် ပြောင်းတယ်

ပထမနေ့ error တွေကို ရှာဖွေဖြေရှင်းခြင်း

Messageအကြောင်းရင်းဖြေရှင်းနည်း
bash: dart: command not foundSDK မရှိဘူး ဒါမှမဟုတ် bin က PATH ထဲ မပါဘူးInstall လုပ်၊ PATH ထဲ ထည့်ပြီး terminal အသစ် ဖွင့်ပါ
Could not find file `hello.dart`Folder မှား သို့မဟုတ် file နာမည်မှားcd နဲ့ file ရှိတဲ့ folder ထဲ ဝင်ပြီး ls ဒါမှမဟုတ် dir နဲ့ စစ်ပါ
Found no `pubspec.yaml` fileProject အပြင်မှာ 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 ထုတ်ပါ
check.dart
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 ထဲ ထည့်ပါ။

အဖြေ ကြည့်ရန်
check.dart
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

SDK ကို install လုပ်ပြီးနောက် terminal အသစ်မှာ dart --version အလုပ်လုပ်ပေမယ့် ဖွင့်ထားပြီးသား terminal အဟောင်းမှာ မလုပ်ဘူး။ ဘာကြောင့်လဲ။

Quick Quiz 2 / 3

ဘယ် command က hello_app ဆိုတဲ့ Dart project folder အသစ်ကို ဆောက်ပေးလဲ။

Quick Quiz 3 / 3

dart create hello_app နဲ့ ရတဲ့ project ရဲ့ အပေါ်ဆုံး folder ထဲမှာ dart run လုပ်ရင် ဘယ် file ကနေ စ run လဲ။

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

  • 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

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

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

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

Get Started | Thuta Learning