const_deferred_class
延遲載入的類不能用 'const' 建立。
描述
#當一個從使用延遲匯入 (deferred import) 方式匯入的庫中的類被用來建立一個 const 物件時,分析器會生成此診斷。常量在編譯時求值,而延遲庫中的類在編譯時不可用。
欲瞭解更多資訊,請參閱延遲載入庫。
示例
#以下程式碼會產生此診斷,因為它嘗試從一個延遲庫建立一個類的 const 例項
dart
import 'dart:convert' deferred as convert;
const json2 = convert.JsonCodec();常見修復
#如果不需要該物件是常量,則更改程式碼以建立非常量例項
dart
import 'dart:convert' deferred as convert;
final json2 = convert.JsonCodec();如果該物件必須是常量,則從 import 指令中移除 deferred
dart
import 'dart:convert' as convert;
const json2 = convert.JsonCodec();