dart (4 flutter)

이우철·2025년 8월 16일
  1. 규정형 변수

bool isCheck = false;
int num1 = 1;
double num2 = 1.11;
// type check
print(num1 is int); // true
print(num2 is int); // false
print(num1.runtimeType); // type check print
// num type
num num3 = 10;
num num4 = 10.11;

  1. type 추론

var i=10; // int
var j=10.1; // double
var str = 'hi'; // String

  1. 상수
    final String name = 'john';
    name='kitty'; // error

const int age = 1;
// const는 컴파일시 결정되는 상수 > 성능상 이점 가져갈 수 있음
// final은 런타임에 결정되는 상수

  1. null safty
    int age = null; // x
    int? nullableAge = null // O
    print(nullableAge ==null) // true

String? nname;
String result = nname; // X - type 다름

if(nname !=null ) {
result = nname; // OK
}

  1. 조건문
    자바 등과 동일

  2. 반복문
    자바와 for, while, do while 문과 동일

  3. 연산자
    String? sttr;

str ?? = 'hello';
// =
str = str??'hello';
// 왼쪽 str이 null이면 'hello'를 취함
// 결국 str이 null이면 hello를 넣어주고 아니면 그대로 사용함

// 3항연산자
var doneCheck = isDone == ture ? '완료' : '진행중';

//cascade 연산자 (..)는 객체를 생성하거나 수정할 때 여러 속성이나
// 메서드를 연속적으로 호출할 수 있게 해주는 문법. 간단하고 실용적임
// 일반적인 객체 생성 및 수정
Bottle water = Bottle(capacity: 500);
water.name = 'Evian';
water.printInfo();

// cascade 연산자 사용
Bottle icis = Bottle(capacity: 300)
..name = 'samdasu'
..updateName('Samdasu Premium')
..printInfo();

// type casting
num i =1;
int j=i; // x type 다름
int j = i as int; // type casting

num k=10.1;
int l = k as double;

  1. Function

/ 자바와 동일
// 단, 인자부분 중괄호로 감싸 named parameter 구성 가능
int sum({int i=0, int j=0}) {
reuturn i+j;
}
// i 나 j를 넣지 않아도 됨 (optional)
// sum method의 오버로드 역할

// lambda 형식
int sum(i,j) => i+j;

// return type 생략 가능 (추천하지 않음, 명시적 리턴 타입 쓰는게 좋음)

// 일급함수 (first-class function)
// g함수를 다른 변수와 동일하게 취급
print(calc(1,2, callback : (i, j) => i*j));

int calc(int i, int j, { required Function(int, int) callback}) {
return callback(i, j);
}

profile
개발 정리 공간 - 업무일때도 있고, 공부일때도 있고...

0개의 댓글