{% seo %}Dart문법(2)-Ternary operator, type casting, function | myHobbyBest
Dart문법(2)-Ternary operator, type casting, function
포스트
취소

Dart문법(2)-Ternary operator, type casting, function

삼항 연산자(Ternary operator)

1
2
    bool isClear = true;
    var weather = isClear == true ? '맑음' : '흐림';

Cascade notation

1
2
3
4
5
6
7
8
    // var paint = Paint();
    // paint.color = 'black';
    // paint.strokeCap = ;

    var paint = Paint()  // ';' 를 붙이지 않는다. 
    ..color = 'black'
    ..strokeCap = ;

type casting

1
2
3
4
5
6
    num i = 10;
    int ii = i as int;  // type 이 달라서 오류날 때 강제로 type을 일치시킨다.

    num d = 10.5;
    double dd = d as double; 

function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    void main(){
        print (sum1(10, 20)); //  30
        print (sum2()); //  0
        print (sum2(a: 5)); //  5

    }

    int sum1(int a, int b){  // return-type,method-name, 입력매개변수
        return a + b; //, 함수내용 또는 return
    }

    int sum2({int a = 0, int b = 0}){  // named parameter
        return a + b; 
    }

    void main (){
        print ( calc(10,5, (a,b) => a+b)); // 함수를 파라메터로 받을 수 있다.
    }

    int calc(int a, int b , function(int, int) callback) {
        return callback(a,b);
    }

함수를 named parameter로 넣은 경우 (Flutter에서 많이 보게될 case)

1
2
3
4
5
6
7
8
9
10
11
12
13
     void main (){
        print (calc(
            10,
            5, 
            callback: (a,b){ // 함수를 named parametr 로 넣은 경우
                return a + b;
            },
        ));
    }

    int calc(int a, int b ,{ required function(int, int) callback} ) {
        return callback(a,b);
    }
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.

Dart문법(1) - type, null-safty, loop

Dart문법(3) - List

Comments powered by Disqus.