跳到主內容

definitely_unassigned_late_local_variable

late 區域性變數 '{0}' 在此處肯定未被賦值。

描述

#

確定賦值分析表明一個被標記為 late 的區域性變數在其被賦值之前就被讀取時,分析器會產生此診斷資訊。

示例

#

以下程式碼產生此診斷資訊,因為 x 在被讀取之前未被賦值

dart
void f(bool b) {
  late int x;
  print(x);
}

常見修復方法

#

在讀取變數之前為其賦值

dart
void f(bool b) {
  late int x;
  x = b ? 1 : 0;
  print(x);
}