簡體   English   中英

Dart 沒有為 class 定義方法“setRng”

[英]Dart The method 'setRng' isn't defined for the class

我做了一個 class 不知道為什么我的吸氣劑很好,但二傳手顯示為 flutter 中未定義。

class Test {
  int range = 1000;

  set setRng(int val) => range = val;

  get getRng => range;
}

Test.getRng沒有錯誤..

Test.setRng(100)拋出錯誤The method 'setRng' isn't defined for the class 'Test'

顯然他們都被定義了..?

在 Dart 中,您使用setter = value; . 所以你的代碼可以這樣修改:

test.setRng = 0;

Getter 和 setter 使成員函數看起來像成員變量。 按照慣例,它們可以具有類似名稱的變量,例如:

class Test {
  int _range = 1000;

  set range(int val) => _range = val; // optionally perform validation, etc

  int get range => _range;
}

現在使用時看起來更自然:

test.range = 123; // using the setter
print(test.range); // using the getter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM