본문 바로가기
Dart

20210817#(81) 플러터를 위한 Dart

by zho 2021. 8. 17.

flex 1 : 1
SizeBox

Matarial
Scaffold
SafeArea
Spacer 1 : 1 : 1


Dart

1. Var, Dynamic type

다이나믹은 둘 다 가능

void main(){
    dynamic name = 'code';
    name = 1;
}

또는 이것도 가능

void main(){
    var name;
    name = 'code'
    name = 1
}


2. List type

○ List 선언 방법
List a = [];
List a = new List();


a.add('jiho');
a.add('jihoho');
a.removeAt(1); // -> index 1번 위치한 jihoho 삭제

List<String> a = [];
a.add('jiho')
a.add(1); // error int type can't assigned to String type

Growable List = 계속 커질 수 있는 List

List<String> food = new List(3); // -> [null, null, null] total 3 value
food.add('pizza'); -> Error Unsupported operation 
food.removeAt(0) -> Error Unsupported operation


List a = new List.from([
    '1',
    '2',
    '3',
]);
-> [1,2,3]

a.length = 3



3. Map Type - key value Pair - Map은 key값 씀
Map dictionary = {
    'apple': '사과',
    'banana' : '바나나', //:기준 왼쪽 key 오른쪽 Value
};

dictionary.addAll({
    'apple':'사과',
});

dictionary.remove('apple')

dictionary['banana'] = '맛있어';

Map dictionary = {}
Map dictionary = new Map();
Map dictionary = new Map.from({
    'apple' : '사과',
});

print(dictionary.keys.toList()); // key 값만 출력
print(dictionary.values.toList()); // values 값만 출력

Map<String, int> price = {
    'apple' : 2000,
};

Map에서 키는 1개만 존재


4.Final, Const

final String name = 'code'; 런타임에 값이 지정되도 상관 x 
const String name = 'code'; 컴파일이 되는 순간에 값이 지정되어있어야 함. 런타임에 값이 지정된다면 const는 사용불가
 
둘다 이후에 변경 불가



5. Operators

num ?? = 4; -> 값이 null일때만 할당하는 방법

int num = 1;
print(num is int); -> true else: false (is!도 가능)


6. Enum
//승인 - approved
//반려 - rejected
//대기 - pending

enum Status {
    approved,
    rejected,
    pending,
}

Status status = Status.approved; (오타 날 일, 위험성 적어짐)
print(Status.values.toList()); //-> [Status.approved, Status.rejected, Status.pending]


6. Function

int addList(List testList, int a, [int b = 3]){} // -> default b값 입력 안하면 3을 넣어라

int addList(List testList, int a, {
    int b,
    int c,
    int d,
}) 
// -> 파라미터가 너무 많고 정리가 힘들 때 쓰는 방법 : 네임드 파라미터 (순서 상관 x)
호출할때는 addList(testList2, 1, a : 3,)


7. Typedef - 함수와 밀접 (함수를 변수처럼 사용가능)
void main(){
    calculate(1, 2, add); -> x 더하기 y 는 3입니다.
}

typedef Operation(int x, int y);

void add(int x, int y){
    print('x 더하기 y 는 ${x+y}입니다.');

void subtract(int x, int y){
    print('x 빼기 y 는 ${x-y}입니다.');
}

void calculate(int x, int y, Operation oper){
    oper(x,y);
}
























728x90