import 'package:http/http.dart' as http;
//pub.dev์์ http ํจํค์ง ๋ค์ด ๋ฐ๊ธฐ
//ํจํค์ง๋ฅผ http๋ก
var person='์ฑ๋ฏผ';
//ํ์
์ถ๋ก
//ํ์
๋ณ๊ฒฝ ๋ถ๊ฐ (์ด๊น๊ฐ์ ๊ธฐ์ค์ผ๋ก ํด๋น ๋ณ์์ ํ์
์ ์ถ๋ก ํ๊ธฐ ๋๋ฌธ์
ํ๋ฒ var๋ก ์ ์ธํ ๋ณ์์ ๋ค๋ฅธ ํ์
์ ๊ฐ์ ๋ฃ์ด์๋ ์๋๋ค.)
//์ฌ์ฉ์ ๊ถ์ฅํ์ง ์์
dynamic age=22;
//ํ์
์ถ๋ก
//ํ์
๋ณ๊ฒฝ ๊ฐ๋ฅ
๊ณตํต์ : ํ๋ฒ ์ ์ธํ๋ฉด ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.
์ฐจ์ด์ :
final c=DateTime.now();
//final๋ก ์ ์ํ ๊ฐ์ ๋ฐํ์์์ ์ ์๋ ๊ฐ์ผ๋ก ์ค์ ํ ์ ์๋ค.
const d=DateTime.now(); //์๋ฌ ๋ฐ์
//const๋ก ์ ์ํ ๊ฐ์ ๋ฐํ์์์ ์ ์๋ ๊ฐ์ ์ค์ ํ ์ ์๋ค.
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;
'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);
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
}
null safety๋ฅผ ์ํด ์ถ๊ฐ๋ ํค์๋๋ค! (non-nullable)
void hello({required String name}){
//{}๊ฐ ์์ผ๋ฏ๋ก named parameter์ด์ optional parameter์ด์ง๋ง
//required ํค์๋๊ฐ ์ด๋ณด๋ค ๋ ์ฐ์ ์๋๊ธฐ์ ํด๋น ํ๋ผ๋ฏธํฐ๋ค์ ํ์์ ์ผ๋ก ์์ฑ๋์ด์ผ ํ๋ค.
print('Hello, $name');
}
//์ต์ด ์ ์ธ์์๋ null์ ๊ฐ์ง๋ง ํ๋ฒ ์ ์๋ ์ดํ์๋ null์ ๊ฐ์ง์ง ์์๋ ๋๋ ๊ฒฝ์ฐ
Late String _value;
โ๏ธ ๋ณ์๋ฅผ ์ด๊ธฐํ ์์ด ์ ์ธํ ๋๋, null ๊ฐ์ ๋ํ ์์ ์ฑ์ ๋ณด์ฅํ๊ธฐ ์ํด ํ์ ์ nullable๊ณผ non-nullable๋ก ๊ตฌ๋ถํด ์ค์ผ ํ๋ค.
(๋ฐ๋์ ํ์ํ ๋งค๊ฐ๋ณ์๋ required๋ก ์ง์ ํด์ฃผ๊ธฐ)
positional / named, (default)
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);
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
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
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();
}
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!: ๊ฐ์ฒด์ ํ์ ์ ํ์ธํด true/false ๋ฐํ
void main() {
var age = 0;
print(age is int); //true
print(age is num); //true
print(age is! num); //false
}
ํ๋ฌํฐ Widget ์์์๋ if๋ฌธ์ ์ฌ์ฉํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์ผํญ ์ฐ์ฐ์๋ก ์์ฑํด์ผ ํ๋ค. ๋ฐ๋ผ์ ํ๋ฌํฐ์์ ๊ต์ฅํ ์์ฃผ ์ฌ์ฉ๋๋ ๋ฌธ๋ฒ์ด๋ค.
Icon(isWeekCalendarVisible ? Icons.arrow_drop_down : Icons.arrow_drop_up)
Container(
decoration: BoxDecoration(
color: isInverted ? Colors.white : Colors.black),
)
//๋ฌผ์ํ(?) ์์ ์์์ด true ์ด๋ฉด ์์ชฝ์ ๋ฐํํ๊ณ , false ์ด๋ฉด ๋ค์ชฝ์ ๋ฐํํ๋ค.
โ ์ผํญ์ฐ์ฐ์๋ ๋ฐ๋์ true์ false ๋๊ฐ์ง ๋ฐํ๊ฐ์ด ํ์ํ๋ฐ, ์๋ฌด ์์ ฏ๋ ๋ฐํํ๊ณ ์ถ์ง ์์ ๊ฒฝ์ฐ์๋ SizedBox๋ฅผ ๋ฐํํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค.
List<String> fruits = ['์ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋', '๊ทค', '๋ธ๊ธฐ'];
int i = 0;
while (i<fruits.length) {
print('๋๋ ${fruits[i]}๋ฅผ ์ข์ํด');
i++;
}
List<String> fruits = ['์ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋', '๊ทค', '๋ธ๊ธฐ'];
for (int i = 0; i<fruits.length; i++) {
print('๋๋ ${fruits[i]}๋ฅผ ์ข์ํด');
}
List<String> fruits = ['์ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋', '๊ทค', '๋ธ๊ธฐ'];
fruits.forEach((fruit){
print('๋๋ $fruit์ ์ข์ํด');
});
//fruits.forEach((fruit)=>print('๋๋ $fruit์ ์ข์ํด')); ๋ก๋ ํํํ ์ ์๋ค.
List<String> fruits = ['์ฌ๊ณผ', '๋ฐฐ', 'ํฌ๋', '๊ทค', '๋ธ๊ธฐ'];
for (String fruit in fruits){
print('๋๋ $fruit์ ์ข์ํด');
}
Dart์์๋ List, Set, Map ๋ฑ์ Collection์ ์ ๊ณตํ์ง๋ง ๋ฐฐ์ด์ ์๋ฃ๊ตฌ์กฐ๋ ์ ๊ณตํ์ง ์๋๋ค.
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
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
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