GlobalAssert는 상수에 대한 빌드 시 어설션을 가능하게 하는 가벼운 Dart 패키지로, 런타임이 아닌 빌드 프로세스 중에 구성 오류를 조기에 포착할 수 있도록 도와줍니다. 중요한 값을 컴파일 시점에 검증함으로써 앱의 신뢰성을 향상시키고, 생산 환경에서 침묵하는 실패의 위험을 줄입니다. 정적 데이터에 불변 조건을 강제하기에 적합하며, 모든 주요 플랫폼의 Flutter 프로젝트에 원활하게 통합될 수 있습니다.
사용 사례
구성 상수 검증
잘못된 구성으로 인한 런타임 오류 방지
빌드 중에 불변 검사를 강제 적용하기
테스트 신뢰성 향상
조기 유효하지 않은 상태 탐지
안전한 초기화 보장
주요 기능
빌드 시 시간에 대한 주장 평가
상수 검증을 위한 간단한 API
런타임 오버헤드 없음
크로스 플랫폼 지원
Flutter 빌드 시스템과의 통합
최소 종속성 풋프린트
적합한 대상
빌드 시간 안정성을 우선시하는 개발자
품질 관리를 위해 정적 분석을 사용하는 팀
엄격한 구성 요구 사항을 가진 프로젝트
강력한 초기화 검사를 필요로 하는 Flutter 앱
조기 실패 탐지가 필요한 CI/CD 파이프라인
고려 사항
컴파일 타임 상수에서만 작동합니다
간단한 부울 조건에 한정
런타임 검증을 대체하지 않습니다
빌드 시 실행에 대한 이해가 필요합니다
빌드 시간을 약간 증가시킬 수 있습니다
동적 또는 사용자 제공 데이터에 적합하지 않습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
A micro-library for asserting at build time.
It's a common practice to configure a Dart app using const (build-time) values. There are benefits to this approach, such as enabling tree shaking of unused code.
This library makes it easy to do build-time assertions about such constants so that misconfigured apps fail at build-time instead of failing at runtime.
Usage
Use this library to validate your app's const values at build time. Anything can be annotated with @Assert.
//// This app will fail compilation with '--define=MY_VAL=foo'//@Assert(
myBuildTimeVal == 'val1' || myBuildTimeVal == 'val2',
'myBuildTimeVal must be one of "val1" or "val2"',
)
const myBuildTimeVal = String.fromEnvironment('MY_VAL');
voidmain() {
print('myBuildTimeVal is "$myBuildTimeVal"');
}
FAQ
🤔 How does this even work?!
The Dart compiler invokes annotation constructors at compile-time. This micro-library takes advantage of that to "trick" the compiler into allowing build-time asserts that can be placed (almost) anywhere. Special thanks to Kyle Turney for the inspiration behind this approach.
Why not allow an assert without a message?
Unfortunately, Dart doesn't treat an assert with a null message the same as an assert without any message at all. This means that we would need two constructors for @Assert to enable both messageless asserts and asserts with messages. Instead, we made the design choice when creating this micro-library that @Assert should require a non-null message. This enforces more meaningful compilation failure messages.