Created at : 2025-01-07 19:29
Auther: Soo.Y
๐๋ฉ๋ชจ
1. Data Types
Dart์์ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- int: ์ ์ (์:
1
,42
) - double: ๋ถ๋ ์์์ ์ซ์ (์:
3.14
,-0.5
) - num:
int
์double
์ ๋ชจ๋ ํฌํจํ๋ ํ์ . - String: ๋ฌธ์์ด (์:
'hello'
,"Dart"
) - bool: ์ฐธ/๊ฑฐ์ง ๋
ผ๋ฆฌ๊ฐ (์:
true
,false
)
์ค์ต ๋ฌธ์ 1: ๋ณ์ a
, b
, c
, d
, e
๋ฅผ ๊ฐ๊ฐ int, double, num, String, bool ํ์
์ผ๋ก ์ ์ธํ๊ณ ๊ฐ์ ์ด๊ธฐํํ์ธ์.
int a = 10;
double b = 3.14;
num c = 5.5; // num์ int์ double ๋ชจ๋๋ฅผ ํฌํจํฉ๋๋ค.
String d = 'Hello, Dart!';
bool e = true;
์ค์ต ๋ฌธ์ 2: ๊ฐ ๋ณ์์ ํ์
์ ์ถ๋ ฅํ์ธ์ (runtimeType
์ฌ์ฉ)
void main() {
int a = 10;
double b = 3.14;
num c = 5.5;
String d = 'Hello, Dart!';
bool e = true;
print('a์ ํ์
: ${a.runtimeType}');
print('b์ ํ์
: ${b.runtimeType}');
print('c์ ํ์
: ${c.runtimeType}');
print('d์ ํ์
: ${d.runtimeType}');
print('e์ ํ์
: ${e.runtimeType}');
}
2. Lists
Dart์ ๋ฆฌ์คํธ๋ Python์ ๋ฆฌ์คํธ์ ์ ์ฌํ๋ฉฐ, ์ฌ๋ฌ ๊ฐ์ ํ๋์ ๋ฐฐ์ด์ ์ ์ฅํฉ๋๋ค.
- ๊ณ ์ ๊ธธ์ด: ๊ธธ์ด๋ฅผ ๋ณ๊ฒฝํ ์ ์์.
- ๊ฐ๋ณ ๊ธธ์ด: ์ถ๊ฐ ๋ฐ ์ญ์ ๊ฐ๋ฅ.
์ค์ต ๋ฌธ์ 1: ๊ธธ์ด๊ฐ ๊ณ ์ ๋ List<int>
๋ฅผ ๋ง๋ค๊ณ ์ซ์ 1~5๋ฅผ ์ ์ฅํ์ธ์.
void main() {
List<int> fixedList = List<int>.filled(5, 0); // ๊ธธ์ด๊ฐ 5์ธ ๊ณ ์ ๋ฆฌ์คํธ ์์ฑ
fixedList[0] = 1;
fixedList[1] = 2;
fixedList[2] = 3;
fixedList[3] = 4;
fixedList[4] = 5;
print(fixedList);
}
์ค์ต ๋ฌธ์ 2: ๊ฐ๋ณ ๊ธธ์ด ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ , ๋ฌธ์์ด ๊ฐ 3๊ฐ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ ฅํ์ธ์.
void main() {
List<String> growableList = []; // ๊ฐ๋ณ ๊ธธ์ด ๋ฆฌ์คํธ ์์ฑ
growableList.add('Apple');
growableList.add('Banana');
growableList.add('Cherry');
print(growableList);
}
์ค์ต ๋ฌธ์ 3: ๋ฆฌ์คํธ์ ์์๋ฅผ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ํํ์ฌ ์ถ๋ ฅํ์ธ์.
void main() {
List<String> fruits = ['Apple', 'Banana', 'Cherry'];
for (String fruit in fruits) {
print(fruit);
}
}
3. String Interpolation
๋ฌธ์์ด ์์์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ฌธ์์ด ๋ณด๊ฐ๋ฒ์ด๋ผ๊ณ ํฉ๋๋ค.
$๋ณ์๋ช
๋๋${ํํ์}
ํ์์ผ๋ก ์ฌ์ฉ.
์ค์ต ๋ฌธ์ 1: ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ์ฅํ๋ ๋ณ์ name
, age
๋ฅผ ์ ์ธํ๊ณ , "My name is [name] and I am [age] years old"
๋ฅผ ์ถ๋ ฅํ์ธ์.
void main() {
String name = 'Alice';
int age = 25;
print('My name is $name and I am $age years old');
}
4. Collection(์ปฌ๋ ์
) for
Dart์ for
ํค์๋๋ ์ปฌ๋ ์
(๋ฆฌ์คํธ, ์ธํธ ๋ฑ)์ ์ํํ๋ฉฐ ์์๋ฅผ ์์ฑํ ๋ ์ ์ฉํฉ๋๋ค.
์ค์ต ๋ฌธ์ 1: ์ซ์ 1~5๊ฐ ํฌํจ๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๊ณ , ๊ฐ ์ซ์์ ์ ๊ณฑ ๊ฐ์ ์๋ก์ด ๋ฆฌ์คํธ๋ก ๋ง๋์ธ์.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<int> squaredNumbers = [for (int number in numbers) number * number];
print(squaredNumbers); // ์ถ๋ ฅ: [1, 4, 9, 16, 25]
}
์ค์ต ๋ฌธ์ 2: ๋ฌธ์์ด ๋ฆฌ์คํธ์์ ๊ธธ์ด๊ฐ 4 ์ด์์ธ ์์๋ง ํฌํจ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋์ธ์.
void main() {
List<String> words = ['dart', 'flutter', 'ui', 'ux'];
List<String> longWords = [for (String word in words) if (word.length >= 4) word];
print(longWords); // ์ถ๋ ฅ: ['dart', 'flutter']
}
5. Maps
๋งต์ ํค์ ๊ฐ์ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
- Dart์์ ๋งต์
Map<KeyType, ValueType>
์ผ๋ก ์ ์ธ.
์ค์ต ๋ฌธ์ 1: ๊ตญ๊ฐ์ ์๋๋ฅผ ํค-๊ฐ์ผ๋ก ์ ์ฅํ ๋งต์ ์์ฑํ์ธ์.
- South Korea, Seoul
- Japan, Tokyo
- France, Paris
void main() {
Map<String, String> countryCapital = {
'South Korea': 'Seoul',
'Japan': 'Tokyo',
'France': 'Paris'
};
print(countryCapital);
}
์ค์ต ๋ฌธ์ 2: ๋งต์์ ํน์ ๊ตญ๊ฐ์ ์๋๋ฅผ ๊ฒ์ํ๊ณ ์ถ๋ ฅํ์ธ์.
void main() {
Map<String, String> countryCapital = {
'South Korea': 'Seoul',
'Japan': 'Tokyo',
'France': 'Paris'
};
String capitalOfJapan = countryCapital['Japan'] ?? 'Unknown';
print('The capital of Japan is $capitalOfJapan');
}
์ค์ต ๋ฌธ์ 3: ๋ชจ๋ ํค์ ๊ฐ์ ์ํํ๋ฉฐ ์ถ๋ ฅํ์ธ์.
- Map์
forEach
์ฌ์ฉ
void main() {
Map<String, String> countryCapital = {
'South Korea': 'Seoul',
'Japan': 'Tokyo',
'France': 'Paris'
};
countryCapital.forEach((country, capital) {
print('The capital of $country is $capital');
});
}
6. Sets
Dart์ ์ธํธ๋ ์ค๋ณต๋์ง ์๋ ๊ณ ์ ํ ๊ฐ๋ค์ ์งํฉ์ ๋๋ค.
์ค์ต ๋ฌธ์ 1: ์ซ์ 1, 2, 3, 3, 4, 5๋ฅผ ํฌํจํ ์ธํธ๋ฅผ ์์ฑํ๊ณ ์ถ๋ ฅํ์ธ์ (์ค๋ณต์ด ์ ๊ฑฐ๋๋์ง ํ์ธ).
void main() {
// ์ค๋ณต๋ ๊ฐ์ด ํฌํจ๋ ๋ฆฌ์คํธ
List<int> numbers = [1, 2, 3, 3, 4, 5];
// ๋ฆฌ์คํธ๋ฅผ ์ธํธ๋ก ๋ณํํ์ฌ ์ค๋ณต ์ ๊ฑฐ
Set<int> numberSet = Set<int>.from(numbers);
// ์ธํธ ์ถ๋ ฅ
print(numberSet); // ์ถ๋ ฅ: {1, 2, 3, 4, 5}
}
์ค์ต ๋ฌธ์ 2: ๋ ์ธํธ๋ฅผ ๋ง๋ค์ด ํฉ์งํฉ, ๊ต์งํฉ, ์ฐจ์งํฉ์ ๊ตฌํ์ธ์.
- union : ํฉ์งํฉ ๋ฉ์๋
- intersection : ๊ต์งํฉ ๋ฉ์๋
- difference : ์ฐจ์งํฉ ๋ฉ์๋
void main() {
// ์ฒซ ๋ฒ์งธ ์ธํธ
Set<int> setA = {1, 2, 3, 4, 5};
// ๋ ๋ฒ์งธ ์ธํธ
Set<int> setB = {4, 5, 6, 7, 8};
// ํฉ์งํฉ: ๋ ์ธํธ์ ๋ชจ๋ ์์๋ฅผ ํฌํจ (์ค๋ณต ์ ๊ฑฐ)
Set<int> unionSet = setA.union(setB);
print('ํฉ์งํฉ: $unionSet'); // ์ถ๋ ฅ: {1, 2, 3, 4, 5, 6, 7, 8}
// ๊ต์งํฉ: ๋ ์ธํธ์ ๋ชจ๋ ์กด์ฌํ๋ ์์
Set<int> intersectionSet = setA.intersection(setB);
print('๊ต์งํฉ: $intersectionSet'); // ์ถ๋ ฅ: {4, 5}
// ์ฐจ์งํฉ: setA์๋ ์๊ณ setB์๋ ์๋ ์์
Set<int> differenceSet = setA.difference(setB);
print('์ฐจ์งํฉ: $differenceSet'); // ์ถ๋ ฅ: {1, 2, 3}
}