搜尋套件、儲存庫與 APP 分類

套件詳情v1.0.0

global_assert

一個微型程式庫,可輕鬆在建構時間對常數進行斷言,讓錯誤設定的應用程式在建構時間失敗,而非在執行時間失敗。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
9570
喜歡數
7
Pub 分數
160

安裝套件

flutter pub add global_assert

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

GlobalAssert 是一個輕量級的 Dart 套件,可於建置時對常數啟用斷言,幫助在建置過程中提早發現設定錯誤,而非在執行時期。透過在編譯時期驗證關鍵值,它提升了應用程式的可靠性,並降低生產環境中靜默失敗的風險。非常適合強制執行靜態資料上的不變條件,可無縫整合至所有主要平台的 Flutter 專案中。

適用情境

  • 驗證配置常量
  • 防止因設定錯誤導致的執行時間錯誤
  • 在建構過程中強制執行不變量檢查
  • 提高測試可靠性
  • 盡早檢測無效狀態
  • 確保安全初始化

主要特色

  • 編譯時期斷言評估
  • 用於常數驗證的簡單 API
  • 無執行時間開銷
  • 跨平台支援
  • 與 Flutter 結構系統整合
  • 最小依賴足跡

適合對象

  • 重視建構時間安全性之開發者
  • 使用靜態分析進行品質控制的團隊
  • 具有嚴格配置要求的專案
  • 需要強大初始化檢查的 Flutter 應用
  • 需要早期失敗檢測的 CI/CD 流水線

使用建議

  • 僅適用於編譯時常數
  • 僅限簡單的布林條件
  • 不取代執行階段驗證
  • 需要理解建構時執行
  • 可能會稍微增加建構時間
  • 不適用於動態或使用者提供的資料

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

A micro-library for asserting at build time.

pub package Build Status Coverage

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');

void main() {
  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.

Will this always be needed?

We expect that when Dart supports metaprogramming, this library will be deprecated.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。