use_null_aware_elements — 컬렉션 리터럴에서는 null-aware 요소를
use_null_aware_elements — 컬렉션 리터럴에서는 null-aware 요소를
컬렉션 리터럴에서 null인지 확인해서 넣는 대신, null-aware 요소(?key)를 쓰면 훨씬 간결해져요.
본문
이 규칙은 컬렉션 리터럴에서 가능하면 null-aware 요소를 사용하라고 권장해요.
BAD:
f(String? key) => {if (key != null) key: "value"};
GOOD:
f(String? key) => {?key: "value"};
if (key != null) key: "value"처럼 null 검사를 매번 해 주는 대신, ?key라고 쓰면 key가 null이 아닐 때만 맵에 항목이 들어가요. ? 하나로 조건부 삽입을 표현하니까 코드가 훨씬 짧고 명확해져요.
더 알아보기
규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 use_null_aware_elements를 추가하면 돼요.
linter:
rules:
- use_null_aware_elements
YAML 맵 문법으로 규칙을 설정한다면 linter > rules 아래에 use_null_aware_elements: true를 쓰면 돼요.
linter:
rules:
use_null_aware_elements: true