πŸ”Ά [μ˜€λ¦„μΊ ν”„_2일차] Dart Language Tour

손세은·2023λ…„ 11μ›” 29일

Dart Language Tour

Untitled

main ν•¨μˆ˜

// 이 Dart μ½”λ“œλŠ” main() ν•¨μˆ˜λ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
// 이 μ½”λ“œλŠ” μ‹€ν–‰ κ°€λŠ₯ν•œ Dart μ½”λ“œμž…λ‹ˆλ‹€.

void main() {
  // main ν•¨μˆ˜ 내뢀에 ν”„λ‘œκ·Έλž¨μ˜ μ‹€ν–‰ λ‘œμ§μ„ μž‘μ„±ν•©λ‹ˆλ‹€.
  print('Hello, Dart!');
  printMessage();
}

void printMessage() {
  print('This is a message.');
}

//flutter: Hello, Dart!
//flutter: This is a message.

variables

// 이 Dart μ½”λ“œλŠ” λ³€μˆ˜ νƒ€μž…κ³Ό λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //λ³€μˆ˜ μ„ μ–Έ
  //μ •μˆ˜ν˜• λ³€μˆ˜
  int number = 10;
  //μ‹€μˆ˜ν˜• λ³€μˆ˜
  double pi = 3.14;
  //λΆˆλ¦¬μ–Έ λ³€μˆ˜
  bool isDartFun = true;
  //λ¬Έμžμ—΄ λ³€μˆ˜
  String greeting = 'Hello, Dart!';
  //리슀트 λ³€μˆ˜
  List<int> numbers = [1, 2, 3, 4, 5];
  //λ§΅ λ³€μˆ˜
  Map<String, int> ages = {
    'Alice': 13,
    'Ryn': 66,
    'James': 18,
  };

  //λ³€μˆ˜ 좜λ ₯
  print(number);
  print(pi);
  print(isDartFun);
  print(greeting);
  print(numbers);
  print(ages);

//flutter: 10
//flutter: 3.14
//flutter: true
//flutter: Hello, Dart!
f//lutter: [1, 2, 3, 4, 5]
//flutter: {Alice: 13, Ryn: 66, James: 18}

  //λ³€μˆ˜κ°’ λ³€κ²½
  number = 100;
  pi = 3.141592;
  isDartFun = false;
  greeting = 'GoodBye, Dart!';
  numbers.add(6);
  ages['Ryn'] = 26;

  //λ³€κ²½λœ λ³€μˆ˜ κ°’ 좜λ ₯
  print(number);
  print(pi);
  print(isDartFun);
  print(greeting);
  print(numbers);
  print(ages);

//flutter: 100
//flutter: 3.141592
//flutter: false
//flutter: GoodBye, Dart!
//flutter: [1, 2, 3, 4, 5, 6]
//flutter: {Alice: 13, Ryn: 26, James: 18}
}

control flow

3-1 conditional statement

if-else

// 이 Dart μ½”λ“œλŠ” 쑰건문 μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //if-esleλ¬Έ: 쑰건에 따라 μ½”λ“œ μ‹€ν–‰ μ—¬λΆ€ κ²°μ •
  int number = 42;
  if (number > 50) {
    print('number is greater than 50.');
  } else if (number < 50) {
    print('number is less than 50.');
  } else {
    print('number is 50.');
  }
}

//flutter: number is less than 50.

switch-case

// 이 Dart μ½”λ“œλŠ” 쑰건문 μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //switch-case λ¬Έ: 닀쀑 λΆ„κΈ° 처리
  String fruit = 'apple';
  switch (fruit) {
    case 'apple':
      print('Apple pie is delicious.');
      break;
    case 'banana':
      print('banana is good for your health.');
      break;
    case 'orange':
      print('orange juice is refreshing.');
      break;
    default:
      print('I like other fruits.');
  }
}

//flutter: Apple pie is delicious.

3-2 loof statement

for-in

// 이 Dart μ½”λ“œλŠ” 반볡문 μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //for-in λ¬Έ: λ¦¬μŠ€νŠΈλ‚˜ 맡의 λͺ¨λ“  ν•­λͺ©μ— λŒ€ν•΄ 반볡
  List<String> fruits = ['apple', 'banana', 'orange'];
  for (String favFruit in fruits) {
    print('I like $favFruit.');
  }
}

while

// 이 Dart μ½”λ“œλŠ” 반볡문 μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //while λ¬Έ: 쑰건이 참인 λ™μ•ˆ μ½”λ“œ 반볡 μ‹€ν–‰
  int count = 0;
  while (count < 5) {
    print('count: $count');
    count++;
  }
}

do-while

// 이 Dart μ½”λ“œλŠ” 반볡문 μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //do-while λ¬Έ: μ½”λ“œλ₯Ό μ΅œμ†Œ ν•œ 번 μ‹€ν–‰ν•˜κ³ , 쑰건이 참인 λ™μ•ˆ μ½”λ“œ 반볡 μ‹€ν–‰
  int count = 0;
  do {
    print('count: $count');
    count++;
  } while (count < 3);
}

Functions

  • μΈμˆ˜μ™€ λ°˜ν™˜ν•˜λŠ” κ°’μ˜ νƒ€μž…μ„ μ§€μ •ν•΄μ£ΌλŠ” 것을 ꢌμž₯ν•œλ‹€.
  • κ°„λ‹¨ν•œ ν•¨μˆ˜λŠ” ν™”μ‚΄ν‘œ ν•¨μˆ˜, 단좕 ꡬ문으둜 λ§Œλ“€ 수 μžˆλ‹€.
  • ν•¨μˆ˜κ°€ λ‹€λ₯Έ ν•¨μˆ˜μ˜ μΈμˆ˜λ‘œλ„ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.
void main() {
// 리턴 및 인수 νƒ€μž…μ΄ μ§€μ •λœ ν•¨μˆ˜
  int add(int a, int b) {
    return a + b;
  }

// ν™”μ‚΄ν‘œ ν•¨μˆ˜
  int multiply(int a, int b) => a * b;

//forEach() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œ ν•¨μˆ˜ μΈμˆ˜μ™€ 읡λͺ… ν•¨μˆ˜
  var numbers = [1, 2, 3, 4, 5];
  numbers.where((number) => number.isEven).forEach(print);
  //ν•¨μˆ˜κ°€ λ‹€λ₯Έ ν•¨μˆ˜μ˜ 인수둜 μ‚¬μš©λ  μˆ˜λ„ μžˆλ‹€.
  //printκ°€ forEach의 인수둜 μ‚¬μš©λ¨.
  //where: νŠΉμ • 쑰건에 λ§žλŠ” μš”μ†Œλ“€λ§Œ λͺ¨μ•„μ„œ μƒˆλ‘œμš΄ 리슀트λ₯Ό λ§Œλ“œλŠ” λ©”μ„œλ“œ
  //isEven: μˆ«μžκ°€ μ§μˆ˜μΈμ§€ ν™•μΈν•˜λŠ” λ©”μ„œλ“œ
  //forEach(print): ν•„ν„°λ§λœ 각각의 μš”μ†Œμ— ν•¨μˆ˜ μ‹€ν–‰, ν”„λ¦°νŠΈ
}
  • .where λ©”μ„œλ“œ : νŠΉμ • 쑰건에 λ§žλŠ” μš”μ†Œλ“€λ§Œ λͺ¨μ•„μ„œ μƒˆλ‘œμš΄ 리슀트λ₯Ό λ§Œλ“œλŠ” λ©”μ„œλ“œ
  • .isEven λ©”μ„œλ“œ : μˆ«μžκ°€ μ§μˆ˜μΈμ§€ ν™•μΈν•˜λŠ” λ©”μ„œλ“œ
  • forEach λ©”μ„œλ“œ: ν•„ν„°λ§λœ 각각의 μš”μ†Œμ— ν•¨μˆ˜ μ‹€ν–‰
// 이 Dart μ½”λ“œλŠ” ν•¨μˆ˜ μ„ μ–Έκ³Ό ν•¨μˆ˜ μ‚¬μš© μ˜ˆμ‹œλ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

void main() {
  //ν•¨μˆ˜ 호좜
  printMessage('Hello, Dart!');
  //ν•¨μˆ˜ 호좜: λ°˜ν™˜κ°’ μ‚¬μš©
  addNumbers(1, 2);
  //ν•¨μˆ˜ 호좜: 선택적 λ§€κ°œλ³€μˆ˜ μ‚¬μš©
  showInfo('John', age: 27);
  //ν•¨μˆ˜ 호좜: κΈ°λ³Έ λ§€κ°œλ³€μˆ˜ μ‚¬μš©
  printInfo2('John', 27);
}

//ν•¨μˆ˜ μ„ μ–Έ: λ°˜ν™˜κ°’μ΄ μ—†λŠ” ν•¨μˆ˜
void printMessage(String message) {
  print('${message}');
}

//ν•¨μˆ˜ μ„ μ–Έ: 인자 2개λ₯Ό λ°›μ•„ λ”ν•œ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜
int addNumbers(int a, int b) {
  return a + b;
}

//ν•¨μˆ˜ μ„ μ–Έ: 선택적 λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜
void showInfo(String name, {int? age}) {
  print('name: $name');
  if (age != null) {
    print('age: $age');
  }
}

//ν•¨μˆ˜ μ„ μ–Έ: κΈ°λ³Έ λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜
//μœ„μΉ˜ 기반 λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜
void printInfo2(String name, int age) {
  print('name: $name');
  print('age: $age');
}

comments

  • ν•œμ€„ 주석, λ¬Έμ„œν™”μš© 주석, μ—¬λŸ¬ 쀄 주석 ν‘œμ‹œλ₯Ό λͺ¨λ‘ μ§€μ›ν•œλ‹€.
// 이것은 일반적인 ν•œ 쀄 μ£Όμ„μž…λ‹ˆλ‹€.
/// 라이브러리, 클래슀, 그리고 κ·Έλ“€μ˜ 멀버λ₯Ό λ¬Έμ„œν™”ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ” λ¬Έμ„œ μ£Όμ„μž…λ‹ˆλ‹€.

/** 이것은 μ—¬λŸ¬ 쀄 μ£Όμ„μž…λ‹ˆλ‹€.
 *  μ—¬λŸ¬ 쀄 주석은 μ—¬λŸ¬ 쀄 μ£Όμ„μž…λ‹ˆλ‹€.
 *  μ—¬λŸ¬ 쀄 주석은 μ—¬λŸ¬ 쀄 μ£Όμ„μž…λ‹ˆλ‹€.
 */

/* 이와 같은 주석은 
μ—¬λŸ¬ 쀄 주석과
같은 λ°©μ‹μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.
 */

imports

  • λ‹€λ₯Έ λΌμ΄λΈŒλŸ¬λ¦¬μ— μ •μ˜λœ API에 μ•‘μ„ΈμŠ€ ν•˜λ €λ©΄ importλ₯Ό μ‚¬μš©ν•œλ‹€.
  • ν”„λ ˆμž„μ›Œν¬ λ‚΄ 라이브러리 ν˜Ήμ€ μ™ΈλΆ€νŒ¨ν‚€μ§€, ν”„λ‘œμ νŠΈ λ‚΄ νŒŒμΌμ— μ •μ˜λœ API, ν•¨μˆ˜λ₯Ό 뢈러올 수 μžˆλ‹€.
//핡심 라이브러리 κ°€μ Έμ˜€κΈ°
import 'dart:math';
//μ™ΈλΆ€ νŒ¨ν‚€μ§€μ˜ 라이브러리 κ°€μ Έμ˜€κΈ°
import 'package:english_words/english_words.dart';
//파일의 λ‚΄μš© κ°€μ Έμ˜€κΈ°
import 'path/to/my_other_file.dart';
profile
νž™μŠ€ν„° κ°œλ°œμžκ°€ λ λž˜μš”

0개의 λŒ“κΈ€