๐ŸŽฏDart ๋ฌธ๋ฒ•2

์žฅ์ฑ„๋ฏผยท2024๋…„ 2์›” 12์ผ
import 'package:http/http.dart' as http;
//pub.dev์—์„œ http ํŒจํ‚ค์ง€ ๋‹ค์šด ๋ฐ›๊ธฐ
//ํŒจํ‚ค์ง€๋ฅผ http๋กœ 

var์™€ dynamic

var person='์ฑ„๋ฏผ';
//ํƒ€์ž… ์ถ”๋ก 
//ํƒ€์ž… ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ (์ดˆ๊นƒ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์ถ”๋ก ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 
ํ•œ๋ฒˆ var๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์„ ๋„ฃ์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.)
//์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜์ง„ ์•Š์Œ

dynamic age=22;
//ํƒ€์ž… ์ถ”๋ก 
//ํƒ€์ž… ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ 

final๊ณผ const

๊ณตํ†ต์ : ํ•œ๋ฒˆ ์„ ์–ธํ•˜๋ฉด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
์ฐจ์ด์ :

final c=DateTime.now();
//final๋กœ ์ •์˜ํ•œ ๊ฐ’์€ ๋Ÿฐํƒ€์ž„์—์„œ ์ •์˜๋œ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

const d=DateTime.now();  //์—๋Ÿฌ ๋ฐœ์ƒ
//const๋กœ ์ •์˜ํ•œ ๊ฐ’์€ ๋Ÿฐํƒ€์ž„์—์„œ ์ •์˜๋œ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค.

ํƒ€์ž… ์บ์ŠคํŒ…๊ณผ num

int n = 34;
double m = n;  //์—๋Ÿฌ ๋ฐœ์ƒ

int n = 34;
double m = n as double;

int์™€ double ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ƒ์œ„ ํƒ€์ž…์ธ num์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

(num์€ int์™€ double ํ˜•์‹์„ ๋‘˜ ๋‹ค ๋ฐ›์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜จ๋„ ๋ฐ์ดํ„ฐ, ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ ๋ฐ์ดํ„ฐ ๋“ฑ int์™€ doubleํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ ์—†์ด ๋ฐ›์•„์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.)

num a = 34;
num b = 34.8;

assert

  • assert(bool ๊ฐ’);

'bool ๊ฐ’'์ด true์ด๋ฉด ๊ณ„์† ์‹คํ–‰๋˜๋ฉฐ, false์ธ ๊ฒฝ์šฐ ์‹คํ–‰์„ ์ค‘๋‹จํ•œ๋‹ค.

// ๊ฐ’์ด 100๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธ.
assert(number < 100);

// https URL์ธ์ง€ ํ™•์ธ.
assert(urlString.startsWith('https'));

// assert์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด assert ๊ตฌ๋ฌธ ์•ˆ์— ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•ด ์ž‘์„ฑํ•˜๋ฉด ๋จ. 
assert(urlString.startsWith('https'),'URL ($urlString) should start with "https".');

๊ด€๊ณ„ ์—ฐ์‚ฐ์ž (๋น„๊ต์—ฐ์‚ฐ์ž)

assert(2 == 2);
assert(2 != 3);
assert(3 >= 3);
assert(2 <= 3);

null-aware ์—ฐ์‚ฐ์ž(? , ??, !)

  • ?
String _value; 
print(_value);  //null๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๊ธฐ์— ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ

//์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•„ null๊ฐ’์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ?(nullable)๋ฅผ ๋ถ™์—ฌ์ค˜์•ผ ํ•œ๋‹ค.
String? _value;
print(_value); //์ถœ๋ ฅ null
  • ?.
void main(){

	String? name;	//nullable (null ๊ฐ’๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ)
    //๋ฌผ์Œํ‘œ ์—†์ด ์ผ๋ฐ˜ ํƒ€์ž…(non-nullable)์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด 
    //์‚ฌ์šฉ ์ „์— ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐ๊ฐ’์„ ํ• ๋‹นํ•ด ์ค˜์•ผ ํ•œ๋‹ค.
    
    print(name.length);  //์—๋Ÿฌ ๋ฐœ์ƒ
    print(name?.length);  //๋งŒ์•ฝ null์ด๋ฉด null ๋ฐ˜ํ™˜
}
  • ??
void main() {
  int age = 16;
  int myAge = age ?? 20;   //๋งŒ์•ฝ null์ด๋ฉด 20 ๋ฐ˜ํ™˜
  print(myAge);	  //age๊ฐ€ null์ด ์•„๋‹ˆ๋ฏ€๋กœ 16
}
void main(){
	int age;
    int myAge=24;
    myAge??=age;   //age๊ฐ€ null์ด๋ฉด ๋ฐฑ์—…๊ฐ’ ํ• ๋‹น๋จ
    print(myAge);   //์ถœ๋ ฅ 24
}
  • !
    (ํƒ€์ž…๋ช…)! -> non-nullable (null๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์Œ)

required, late

null safety๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ํ‚ค์›Œ๋“œ๋“ค! (non-nullable)

  • required
    ํ•จ์ˆ˜์—์„œ ๋„ค์ž„๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๋‚˜ ์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•  ๋•Œ non-nullable๋กœ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ํƒ€์ž… ์•ž์— ๋ถ™์—ฌ์ค€๋‹ค.
    (ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ, ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅํ•ด์ค˜์•ผ ํ•œ๋‹ค.)
void hello({required String name}){   
//{}๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ named parameter์ด์ž optional parameter์ด์ง€๋งŒ 
//required ํ‚ค์›Œ๋“œ๊ฐ€ ์ด๋ณด๋‹ค ๋” ์šฐ์„ ์‹œ๋˜๊ธฐ์— ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ ํ•„์ˆ˜์ ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค.
	print('Hello, $name');
}
  • late
    ํด๋ž˜์Šค์— non-nullable ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ ๋””ํดํŠธ๊ฐ’์ด๋‚˜ ์ดˆ๊ธฐํ™”๋ฅผ ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋‚˜์ค‘์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ๋ถ™์—ฌ์ค€๋‹ค.
    (์ตœ์ดˆ ์„ ์–ธ์‹œ์—๋Š” null์„ ๊ฐ–์ง€๋งŒ ํ•œ๋ฒˆ ์ •์˜๋œ ์ดํ›„์—๋Š” null์„ ๊ฐ€์ง€์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒฝ์šฐ)
    (๊ฐ’์˜ ์ดˆ๊ธฐํ™”๋ฅผ ๋’ค๋กœ ๋ฏธ๋ฃจ๋ฉฐ, ๊ฐœ๋ฐœ์ž๊ฐ€ null์„ ์‹ค์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์ค€๋‹ค.)
//์ตœ์ดˆ ์„ ์–ธ์‹œ์—๋Š” null์„ ๊ฐ–์ง€๋งŒ ํ•œ๋ฒˆ ์ •์˜๋œ ์ดํ›„์—๋Š” null์„ ๊ฐ€์ง€์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒฝ์šฐ
Late String _value;

โœ”๏ธ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ์—†์ด ์„ ์–ธํ•  ๋•Œ๋Š”, null ๊ฐ’์— ๋Œ€ํ•œ ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํƒ€์ž…์„ nullable๊ณผ non-nullable๋กœ ๊ตฌ๋ถ„ํ•ด ์ค˜์•ผ ํ•œ๋‹ค.


Named Parameter

  1. ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ๊ธฐ
  2. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์ •

(๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” required๋กœ ์ง€์ •ํ•ด์ฃผ๊ธฐ)

Optional parameter

positional / named, (default)

โœ”๏ธ optional positional parameter

  • ์œ„์น˜๊ฐ€ ์ค‘์š”ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ˆœ์„œ๋ฅผ ์ž˜ ์ง€์ผœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
  • []๋กœ ๊ฐ์‹ธ ํ‘œํ˜„ํ•œ๋‹ค. (์ƒ๋žต ๊ฐ€๋Šฅ)
  • ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์˜ต์…”๋„ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
test(int temp1 , [int temp2 , int temp2=10]){
}

test(10 , 20 , 30);
getHttpUrl(String server, String path, [int port=80, int numRetries=3]) {
  // ...
}
getHttpUrl('example.com', '/index.html');
getHttpUrl('example.com', '/index.html', 8080);
getHttpUrl('example.com', '/index.html', 8080, 5);

โœ”๏ธ optional Named parameter

  • ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ˆœ์„œ๋Š” ์ƒ๊ด€์—†์ง€๋งŒ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ ค๋ฉด ์ฝœ๋ก (:)์„ ๊ณก ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
  • {}๋กœ ๊ฐ์‹ธ ํ‘œํ˜„ํ•œ๋‹ค.
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ, ์ƒ๋žตํ•ด๋„ ๋จ
  • ํ•จ์ˆ˜์— ๋งŽ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค„ ๋•Œ ํ˜ผ๋ž€์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•จ์ˆ˜ ์ž‘์„ฑ์‹œ์— ์ˆœ์„œ๋ณด๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์„ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข€ ๋” ์œ ์—ฐํ•œ ํŽธ์ด๋‹ค.
test(int temp1, int temp2, {int temp3}) {
 print('temp1 = $temp1, temp2 = $temp2, temp3 = $temp3');
}

test(10,20,temp3 : 30); //valid & we can see the parameter name is mentioned here.
test(10,20); //also valid

โœ”๏ธ optional default parameter

  • optional named parameter์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๊ธฐ๋ณธ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ง€์ •ํ•ด ๋†“๋Š”๋‹ค.
  • ์ถ”ํ›„์— ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ๋„˜๊ฒจ ๋ฐ›์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ, ๊ธฐ๋ณธ๊ฐ’์ด ๊ทธ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์— ํ•ญ๋‹น๋œ๋‹ค.
test(int temp1, int temp2, {int temp3=10}) {
 print('temp1 = $temp1, temp2 = $temp2, temp3 = $temp3');
} 

test(10,20,temp3 : 30);//valid
test(10,20);//valid

constructor(์ƒ์„ฑ์ž)์™€ ์ธ์Šคํ„ด์Šค

  • ์ƒ์„ฑ์ž
class Human {
    // Instance variables
    String name;
    int hp;
    int mp;
    
    // ์ƒ์„ฑ์ž(constructor)
    Human(String name, int hp, int mp) {
        this.name = name;
        this.hp = hp;
        this.mp = mp;
    }
    
    // ๊ฐ™์€ ์˜๋ฏธ์ด์ง€๋งŒ Human(this.name, this.hp, this.mp) ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.
    
    // Methods
    void attack() {
        print('attack!!');
    }
    
    void defence() {
        print('defence!!');
    }
    
    //์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ธ์Šคํ„ด์Šค์˜ ์ƒ์„ฑ
    Human hero = Human('์ฃผ์ธ๊ณต', 100, 200);

    // ์ด๋ ‡๊ฒŒ hero instance์˜ variable์— ์ ‘๊ทผํ•œ๋‹ค.
    hero.name;
    hero.hp;
    hero.mp;

    // ์ด๋ ‡๊ฒŒ hero instance์˜ method์— ์ ‘๊ทผํ•œ๋‹ค.
    hero.attack();
    hero.defence();
    
    
}

Named Constructor

class WebtoonModel {
	final String title, thumb, id;
    
    WebtoonModel.fromJson(Map<String, dynamic> json)
        : title=json['title'],
          thumb=json['thumb'],
          id=json['id'];
    //named Constructor
    //์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๊ตฌ๋ฌธ์œผ๋กœ ์ƒ์„ฑ์ž์˜ ์ค‘๊ด„ํ˜ธ({}) ์ดํ›„์— ์ž‘์„ฑ๋œ๋‹ค.
}

์œ ํ˜• ํ…Œ์ŠคํŠธ ์—ฐ์‚ฐ์ž: as, is, is!

๋Ÿฐํƒ€์ž„์— ์œ ํ˜•์„ ํ™•์ธํ•˜๋Š” ์—ฐ์‚ฐ์ž

  • as: ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜ ํ‚ค์›Œ๋“œ๋กœ์„œ
    ๋ถ€๋ชจ ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ž์‹ ํƒ€์ž…์˜ ๊ฐ์ฒด๋กœ ํ˜•๋ณ€ํ™˜(=๋‹ค์šด์บ์ŠคํŒ…)ํ•  ๋•Œ ์‚ฌ์šฉ

  • is / is!: ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ํ™•์ธํ•ด true/false ๋ฐ˜ํ™˜

void main() {
  var age = 0;
  print(age is int);	//true
  print(age is num);	//true
  print(age is! num);	//false
}

์กฐ๊ฑด์‹: ์‚ผํ•ญ์—ฐ์‚ฐ์ž

ํ”Œ๋Ÿฌํ„ฐ Widget ์•ˆ์—์„œ๋Š” if๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ”Œ๋Ÿฌํ„ฐ์—์„œ ๊ต‰์žฅํžˆ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.

  • ์กฐ๊ฑด ? A : B
  • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋ฉด A๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด B๋ฅผ ๋ฆฌํ„ด
Icon(isWeekCalendarVisible ? Icons.arrow_drop_down : Icons.arrow_drop_up)

Container(
	decoration: BoxDecoration(
    color: isInverted ? Colors.white : Colors.black),
)

//๋ฌผ์Œํ‘œ(?) ์•ž์˜ ์ˆ˜์‹์ด true ์ด๋ฉด ์•ž์ชฝ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , false ์ด๋ฉด ๋’ค์ชฝ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โž• ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋Š” ๋ฐ˜๋“œ์‹œ true์™€ false ๋‘๊ฐ€์ง€ ๋ฐ˜ํ™˜๊ฐ’์ด ํ•„์š”ํ•œ๋ฐ, ์•„๋ฌด ์œ„์ ฏ๋„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” SizedBox๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค.

์กฐ๊ฑด์‹: ์ด์ค‘๋ฌผ์Œํ‘œ(??)

  • A ?? B
  • A์˜ ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด A๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  null์ด๋ฉด B๋ฅผ ๋ฆฌํ„ด

๋ฐ˜๋ณต๋ฌธ

  1. while๋ฌธ
List<String> fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋„', '๊ทค', '๋”ธ๊ธฐ'];

int i = 0;

while (i<fruits.length) {
    print('๋‚˜๋Š” ${fruits[i]}๋ฅผ ์ข‹์•„ํ•ด');
    i++;
}
  1. for๋ฌธ
List<String> fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋„', '๊ทค', '๋”ธ๊ธฐ'];

for (int i = 0; i<fruits.length; i++) {
    print('๋‚˜๋Š” ${fruits[i]}๋ฅผ ์ข‹์•„ํ•ด');
}

์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ณต๋ฌธ

  1. forEach ๋ฉ”์†Œ๋“œ
  • ํ˜•ํƒœ: .forEach((){});
List<String> fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋„', '๊ทค', '๋”ธ๊ธฐ'];

  fruits.forEach((fruit){
  	print('๋‚˜๋Š” $fruit์„ ์ข‹์•„ํ•ด');
  });
  
  //fruits.forEach((fruit)=>print('๋‚˜๋Š” $fruit์„ ์ข‹์•„ํ•ด')); ๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  1. for (..in..) ๊ตฌ๋ฌธ
  • ์ด ๋ฐฉ์‹์ด 1๋ฒˆ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๋” ์ง๊ด€์ ์ด๋ฉฐ ํ”Œ๋Ÿฌํ„ฐ์—์„œ๋„ ๋” ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค.
List<String> fruits = ['์‚ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋„', '๊ทค', '๋”ธ๊ธฐ'];

for (String fruit in fruits){
	print('๋‚˜๋Š” $fruit์„ ์ข‹์•„ํ•ด');
}

Collections

Dart์—์„œ๋Š” List, Set, Map ๋“ฑ์˜ Collection์„ ์ œ๊ณตํ•˜์ง€๋งŒ ๋ฐฐ์—ด์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

  1. List
  • ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์žˆ๊ณ  ์ค‘๋ณต์„ ํ—ˆ์šฉํ•จ
  • List<ํƒ€์ž…> ๋ณ€์ˆ˜๋ช…=[ ]; //<>์•ˆ์— ์„ ์–ธํ•ด์ค€ ํƒ€์ž…๊ณผ ๋™์ผํ•œ ํƒ€์ž…๋งŒ List์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค
List<String> fruits = ['์‚ฌ๊ณผ', '๋ฐฐ'];

var nums2=[1,2,3];  //ํƒ€์ž… ์ถ”๋ก 

print(setNums[1]);  //์ถœ๋ ฅ 2
print(fruits.length);  //์ถœ๋ ฅ 5

fruits.add('๋”ธ๊ธฐ');  //List์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ
fruits.addAll(['ํฌ๋„', '๊ทค']);  //List์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํžˆ๊ธฐ

fruits.remove('์‚ฌ๊ณผ');  //'์‚ฌ๊ณผ' ๋ฐ์ดํ„ฐ ์‚ญ์ œ
fruits.removeAt(0);  //0๋ฒˆ ์ธ๋ฑ์Šค ๊ฐ’ ์‚ญ์ œ
fruits.removeLast();  //๋งˆ์ง€๋ง‰ ์›์†Œ ์‚ญ์ œ
fruits.removeRange(1,3)  //1๋ฒˆ๋ถ€ํ„ฐ 3๋ฒˆ ์ธ๋ฑ์Šค ์ „๊นŒ์ง€, ์ฆ‰ 1๋ฒˆ๊ณผ 2๋ฒˆ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ์‚ญ์ œ

fruits.clear();  //List์˜ ๋ชจ๋“  ํ•ญ๋ชฉ ์‚ญ์ œ

https://dev-dain.tistory.com/137

  1. Set
  • ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์—†๊ณ  ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ(์ค‘๋ณต ์ž๋™ ์ฒ˜๋ฆฌ)
final Set<String> names={
	'Code Factory',
    'Flutter',
    'inflearn',
};

*Set์— ๊ฐ’ ์ถ”๊ฐ€
names.add('Gina');
names.addAll(['apple','grape','cherry']);
//์ค‘๋ณต๋œ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ด๋„ ํ•ด๋‹น set์—๋Š” ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.

*Set์— ๊ฐ’ ์ œ๊ฑฐ
names.remove('Gina');

*Set์— ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
names.contains('Gina');  //false
names.containsAll(['apple','grape']);  //true
  1. Map
  • ํ‚ค(Key)์™€ ๊ฐ’(Value)๋กœ ๊ตฌ์„ฑ๋œ ํด๋ž˜์Šค
  • ํ‚ค๋Š” ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ’์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•จ
Map<String, bool> isHarryPotter={
	'Harry Potter': true,
    'Ron Weasley': false,
    'Hermione Granger': false,
};
print(isHarryPotter);
//์ถœ๋ ฅ {Harry Potter: true, Ron Weasley: true, Ironman: false}


* Map์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
//๋ฐฉ๋ฒ•1(๊ด„ํ˜ธ ์•ˆ์˜ ๊ฐ’์„ ํ•œ๋ฒˆ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ)
isHarryPotter.addAll({
	'Spiderman': false
});  

//๋ฐฉ๋ฒ•2(์ƒˆ๋กœ์šด key๊ฐ’์— value ์ถ”๊ฐ€)
isHarryPotter['Spiderman']=false;  

//๋ฐฉ๋ฒ•3(Map ๋ณ‘ํ•ฉ)
Map hero={
	'Hulk': false;
}
isHarryPotter.addAll(hero);


*Map์˜ ๊ฐ’ ๋ณ€๊ฒฝ
isHarryPotter['Spiderman']=true;


*Map์˜ ๊ฐ’ ์ œ๊ฑฐ
isHarryPotter.remove('Spiderman');
//๋ณ€์ˆ˜๋ช….remove('์‚ญ์ œํ•  key๊ฐ’');

//Map ์‚ญ์ œํ•˜๊ธฐ
isHarryPotter.clear();


*Map ์ถœ๋ ฅํ•˜๊ธฐ
void main() {
  Map<String, String> gifts = {
    // Key:    Value
    'first': 'partridge',
    'second': 'turtledoves',
    'fifth': 'golden rings'
  };

  // map ์ถœ๋ ฅํ•˜๊ธฐ
  print(gifts);  //์ถœ๋ ฅ๊ฐ’ {first: partridge, second: turtledoves, fifth: golden rings}

  // map ์•ˆ์— ๋ฐ์ดํ„ฐ ์ถœ๋ ฅํ•˜๊ธฐ
  print(gifts['second']);  //์ถœ๋ ฅ๊ฐ’ turtledoves
}


*Map์˜ key๊ฐ’ ๋˜๋Š” value๊ฐ’๋งŒ ๋ชจ์•„๋ณด๊ธฐ/๊ฒ€์ƒ‰
isHarryPotter.keys;
isHarryPotter.values;

isHarryPotter.containsKey('Hulk');   //true

0๊ฐœ์˜ ๋Œ“๊ธ€