non_constant_map_element 진단: const 맵의 요소는 상수여야 해요
non_constant_map_element 진단: const 맵의 요소는 상수여야 해요
Dart 분석기가 만들어 내는 non_constant_map_element 진단은 const 맵 안의 if 요소나 spread 요소가 상수가 아닐 때 알려주는 컴파일 타임 진단이에요. 상수 맵 안에 들어가는 모든 요소는 컴파일 타임에 값이 결정되는 상수여야 해요.
본문
설명
분석기는 상수 맵(const map) 안의 if 요소나 spread 요소가 상수 요소가 아닐 때 이 진단을 만들어요.
예시
다음 코드는 상수가 아닌 맵을 spread하려고 시도하기 때문에 이 진단을 만들어요.
var notConst = <int, int>{};
var map = const <int, int>{...notConst};
마찬가지로, 다음 코드는 if 요소의 조건이 상수 표현식이 아니기 때문에 이 진단을 만들어요.
bool notConst = true;
var map = const <int, int>{if (notConst) 1: 2};
흔한 해결 방법
맵이 상수 맵이어야 한다면, 요소들을 상수로 만들어요. spread 예시에서는 spread되는 컬렉션을 상수로 만들면 돼요.
const notConst = <int, int>{};
var map = const <int, int>{...notConst};
만약 맵이 상수 맵일 필요가 없다면, const 키워드를 제거해요.
bool notConst = true;
var map = <int, int>{if (notConst) 1: 2};