๐Ÿ“ฑ App Development Study/Flutter

[Dart] ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

ibelieveinme 2024. 4. 22. 00:49
728x90

Dart ์ฝ”๋“œ ์˜จ๋ผ์ธ ์ปดํŒŒ์ผ๋Ÿฌ

 

DartPad

 

dartpad.dev

C++ ์ด๋ž‘ ์—„์ฒญ ๋น„์Šทํ•˜๋‹ค ! ์‰ฌ์šด ์–ธ์–ด์˜€๊ตฐ...


*์ถœ๋ ฅ

void main() {
  print('Hello Code Factory');
}

 

*๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ํŠน์ง•

void main() {
  // var : ๋ณ€์ˆ˜ํ˜• ์˜ˆ์ธก ๋ฐ ์ž๋™ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
  var name = 'Dart';
  name = 'newDart';
  print(name.runtimeType);

   // String
  String name2 = 'Dart2';
  
  print('${name} ${name2}');
  print('$name $name2'); // ๊ด„ํ˜ธ ์—†์• ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
  print('$name.runtimeType'); // ๋ณ€์ˆ˜์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ด„ํ˜ธ ํ•„์ˆ˜. *์ถœ๋ ฅ: newDart.runtimeType
  
  // integer
  int i = 10;

  // double
  double d = 10.0;

  // Boolean
  bool isTrue = true;

  // dynamic : var ์ฒ˜๋Ÿผ ๋ชจ๋“  ํ˜• ๊ฐ€๋Šฅ
  // dynamic vs var : var ๋Š” ๋‹ค๋ฅธ ํ˜•์œผ๋กœ ๊ฐ’ ๋ณ€๊ฒฝ ์•ˆ๋˜๊ณ  ํ˜•์ด fix๋จ. dynamic ์€ ๋‹ค๋ฅธํ˜•์œผ๋กœ ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•จ
  dynamic dyn = 'newnewDart';
  dyn = 2; // ๊ฐ€๋Šฅ
  //name = 5; // ์˜ค๋ฅ˜!
}

 

*๋ณ€์ˆ˜ ํ‚ค์›Œ๋“œ

void main() {
  // nullable : null ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
  // non-nullable : null ์ด ๋  ์ˆ˜ ์—†๋‹ค.
  // null : ์•„๋ฌด๋Ÿฐ ๊ฐ’์ด ๋“ค์–ด ์žˆ์ง€ ์•Š๋‹ค.
  String str = 'str';
  //str = null; // error !
  
  String? str2 = 'str2';
  str2 = null; // ok !
  
  // ! : null ์ด ์•„๋‹˜์„ ๋ณด์žฅํ•œ๋‹ค๋Š” ๋œป
  String? str3 = 'str3';
  print(str3!);
  
  // final: final ๋กœ ์„ ์–ธํ•˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  // ๋ณ€์ˆ˜ํ˜• ์ƒ๋žต ๊ฐ€๋Šฅ. final name = 'Dart'; ๋„ ๊ฐ€๋Šฅ.
  // Build ํƒ€์ž„์— ๊ฐ’์„ ์•Œ๊ณ  ์žˆ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  final String name = 'Dart'; 
  print(name);
  //name = 'new Dart'; // error!
  
  // const: const ๋กœ ์„ ์–ธํ•˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  // ๋ณ€์ˆ˜ํ˜• ์ƒ๋žต ๊ฐ€๋Šฅ. const name2 = 'Dart2'; ๋„ ๊ฐ€๋Šฅ.
  // Build ์‹œ๊ฐ„์— ๊ฐ’์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์„ ์–ธ ์ˆœ๊ฐ„์— ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  const String name2 = 'Dart2'; 
  //name2 = 'new Dart'; // error!
  
  // DateTime : ์ฝ”๋“œ ๋ฌธ์žฅ์ด ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„์˜ ์‹œ๊ฐ„ ์ถœ๋ ฅ
  final DateTime now = DateTime.now();
  print(now); // 2024-04-21 23:32:37.310
  //const DateTime now2 = DateTime.now(); // error!
  
  // operator
  double? number = 4.0;
  number ??= 3.0; // number ๊ฐ€ 0 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ๋ผ๋Š” ๋œป.
  print(number); // 4;
  
  // is : ํƒ€์ž… ๋น„๊ต
  print(number is double); // true
  print(number is! String); // true
}

 

*List ์ž๋ฃŒ๊ตฌ์กฐ

void main() {
  //List
  List<String> stringList = ['name1', 'name2', 'name3', 'name4']; 
  List<int> intList = [1, 2, 3, 4];
  
  print(stringList); // [name1, name2, name3, name4]
  print(intList); // [1, 2, 3, 4]

  print(stringList.length); // 4
  print(stringList.reversed); // (name4, name3, name2, name1)
  print(stringList.first); // name1
  print(stringList.indexOf('name1')); // 0
  print(stringList.isEmpty); // false
  //...
 
  stringList.add('name5');
  stringList.remove('name3');
}

 

*Map ์ž๋ฃŒ๊ตฌ์กฐ

void main() {
  // Map
  // Key / Value
  Map<String, String> dictionary = {
    'key1':'value1',
    'key2':'value2',
    'key3':'value3',
  };
  print(dictionary);//{key1: value1, key2: value2, key3: value3}
  
  // ๊ฐ’ ์ถ”๊ฐ€
  // 1๋ฒˆ ๋ฐฉ๋ฒ•
  dictionary.addAll({
    'key4':'value4'
  });
  print(dictionary);//{key1: value1, key2: value2, key3: value3, key4: value4}
  
  // 2๋ฒˆ ๋ฐฉ๋ฒ•
  dictionary['key5'] = 'value5';
  
  // ๊ฐ’ ์ ‘๊ทผ
  print(dictionary['key1']); // value1
  
  // ๊ฐ’ ๋ณ€๊ฒฝ
  dictionary['key1'] = '';
  print(dictionary);//{key1: , key2: value2, key3: value3, key4: value4, key5: value5}
  
  // ๊ฐ’ ์‚ญ์ œ
  dictionary.remove('key1');
  print(dictionary); //{key2: value2, key3: value3, key4: value4, key5: value5}
  
  print(dictionary.keys); //(key2, key3, key4, key5)
  print(dictionary.values); //(value2, value3, value4, value5) 
}

 

*Set ์ž๋ฃŒ๊ตฌ์กฐ

void main() {
  // Set : Map์ด๋ž‘ ๋น„์Šทํ•œ๋ฐ ์ค‘๋ณต์•ˆ๋จ. Map ์€ ๊ฐ’ ์ค‘๋ณต๊ฐ€๋Šฅ.
  final Set<String> names = {
    'name1',
    'name2',
    'name3',
    'name1'
  };
  
  print(names); //{name1, name2, name3} <- ์ด๋ ‡๊ฒŒ ์ค‘๋ณต ์•Œ์•„์„œ ์ง€์›Œ์คŒ.
  
  names.add('name4');
  names.remove('name1');
  
  print(names.contains('name1')); //false
}

 

* ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ(if, for, while)

void main() {
  // if ๋ฌธ
  int number = 3;
  if(number % 3 == 0){
    print('๋‚˜๋จธ์ง€ 0');
  } else if(number % 3 == 1){
    print('๋‚˜๋จธ์ง€ 1');
  } else{
    print('๋‚˜๋จธ์ง€ 2');
  }
  
  // switch
  switch(number % 3){
    case 0:
      print('๋‚˜๋จธ์ง€ 0');
      break;
    case 1:
      print('๋‚˜๋จธ์ง€ 1');
      break;
    default:
      print('๋‚˜๋จธ์ง€ 2');
      break;
  }
  
  // for loop
  for(int i = 0; i < 10; i++){
  }
  
  List<int> numbers = [1,2,3,4,5];
  for(int n in numbers){
  }
  
  // while loop
  int total = 0;
  while(total < 10){
    total += 1;
  }
  print(total); //10
  
  total = 0;
  do{
    total += 1;
  }while(total < 10);
  print(total); //10
}

 

*enum

enum Status{
  approved,
  pending,
  rejected
}

void main() {
  Status status = Status.pending;
  
  switch(status){
    case Status.approved:
      break;
    case Status.pending:
      break;
    case Status.rejected:
      break;
    default:
      print('error');
      break;
  }
}

 

*ํ•จ์ˆ˜

// ํ•จ์ˆ˜
void main() {
  addNumbers(10);
  multipleNumbers(x: 1, y: 2, z: 3);
  multipleNumbers(y: 2, x: 1, z: 3); // ์ด๋ฆ„์ด ์žˆ์–ด์„œ ์ˆœ์„œ ๋ฐ”๊ปด๋„ ์ƒ๊ด€ ์—†์Œ.
  
  int result = getNumber(1);
}

// positional parameter: ๊ธฐ๋ณธ๊ฐ’ ํ•„์š”์—†๊ณ  ํ˜ธ์ถœํ•  ๋•Œ๋„ ๋ณ€์ˆ˜๊ฐ’๋งŒ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•˜๋ฉด ๋จ. ์ˆœ์„œ๋Š” ์ง€์ผœ์•ผ ํ•จ.
// [] : optional parameter. ๊ธฐ๋ณธ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ฑฐ๋‚˜ ? ๋กœ nallable ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•จ.
addNumbers(int x, [int y = 0, int z = 0]){
  int sum = x + y + z;
  print(sum);
}

// named parameter : ์ด๋ฆ„์ด ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ. ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.
// optional parameter ๋„ ๊ฐ€๋Šฅ. ์ดˆ๊ธฐํ™” ํ•˜๊ฑฐ๋‚˜ nullable ์ฒ˜๋ฆฌ๋Š” ํ•ด์ค˜์•ผ ํ•จ.
multipleNumbers({
  required int x,
  required int y,
  int z = 1
}){
  int multiple = x*y*z;
  print(multiple);
}

// return ํ˜• ํ•จ์ˆ˜
int getNumber(int x){
  return x;
}

// arrow function: ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ ๊ฐ€๋Šฅ
int getNumber2(int x,{
  required int y,
  int z = 1
}) => x + y + z;

 

*typedef ํƒ€์ž…์ฐธ์กฐ

//typedef : ํƒ€์ž…์ฐธ์กฐ
void main() {
  Operation operation = add; //๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
  int result = operation(10, 20, 30);
  print(result); //60

  operation = substract; //๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
  int result2 = operation(10, 20, 30);
  print(result2); //-40
  
  int result3 = calculate(30, 40, 50, add);
  print(result3); // 120
}

// ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ดํƒ€์ž…์ด ์ผ์น˜ํ•ด์•ผ ํ•จ
typedef Operation = int Function(int x, int y, int z);

// ๋”ํ•˜๊ธฐ
int add(int x, int y, int z) => x + y + z;

// ๋นผ๊ธฐ
int substract(int x, int y, int z) => x - y - z;

// ๊ณ„์‚ฐ
int calculate(int x, int y, int z, Operation operation) => operation(x, y, z);

 

728x90