[Flutter] 2. Dart (2) - null 처리

김주희's avatar
Jun 08, 2025
[Flutter] 2. Dart (2) - null 처리
오버로딩 지원 안함 → 대신 선택적 매개변수 지원
 
선택적 매개변수 {}
null을 받을 수 있는 타입이돼서 오류가 안남
 
! = .get과 같다 (Optional에서의 .get)
 
자바의 빌더패턴
// null 처리와 선택적 매개변수 void add({int? n1, int? n2}){ print("더하기: ${n1! + n2!}"); } void main(){ add(n1: 3, n2: 4); // key 바인딩 }
 
값이 안들어오면 초기화 가능
void add({int n1 = 1, int n2 = 2}){ print("더하기: ${n1 + n2}"); } void main(){ add(n1: 5); // key 바인딩 }
 
void add({int n1 = 1, int n2 = 2, int n3 = 3}){ // 오버로딩 print("더하기: ${n1 + n2}"); } void main(){ add(n1: 5); }
 
void add({required int n1, required int n2, int? n3}){ print("더하기: ${n1 + n2}"); } void main(){ add(n1: 5); // key 바인딩 }
required는 무조건 값을 받아야하는것
→ 그럼 void add(int n1, int n2, int n3){}와 뭐가 다른데?
그치만 순서 상관없고 key값 넣을 수 있어서 사용
 
html에서 a태그의 핵심속성은 href 나머지는 선택적 매개변수 같은 선택적인 것
// type = 시그니처 void input (String type, {String? placeHolder, String? name}){} void main(){ input("text",placeHolder:"유저네임을 입력하세요"); }
 

 
notion image
 
String은 null을 받지 못한다.
notion image
 
notion image
 
 
notion image
 

1. null 처리

1. ? (null일수도 있어)

notion image
 

2. ! (절대 null이 아니야)

notion image
 

3. ?? (null 대체 연산자)

null이면 뒤에 거를 써라?
notion image
 
최종적으로 실행하는 값 앞에 ?는 별로 → 최종적으로 실행되는 애는 잘 처리해서
notion image
notion image
 
 
 

    notion image
    휴대폰 앱에서 null print 하려고 하면 터지므로 처리 필요
      notion image
      3.
      확신할때만 쓴다.
      notion image
       
       
      핵심은 좌변에 ?를 안붇이기 위한 것
       
      notion image
      ⇒ username이 “ssar”이므로 절대로 null일 수 없음 그치만 username.length라고 하면 빨간불 뜬다. → null 부정 연산자를 쓰자
      ! = 개발자에게 맡기는 것
       
      Share article

      jay0628