[英]Dart class constructor with required arguments while not initializing formal
[英]Dart - Circular dependency while initializing static field
我是 dart 的新手,剛遇到一個我還不明白的問題。 我寫了這個類:
class Currency {
final String symbol;
final String name;
// constants for all available Currencies
static const Currency EURO = const Currency._euro();
static const Currency POUND = const Currency._pound();
static const Currency DOLLAR = const Currency._dollar();
// All available currencies as a list
static const List<Currency> CURRENCIES = const [
EURO,
POUND,
DOLLAR,
];
// Default constructor
Currency(this.symbol, this.name);
// Named constructors
const Currency._euro() : this('€', 'Euro');
const Currency._pound() : this('£', 'British Pound');
const Currency._dollar() : this('\$', 'US Dollar');
// toString()
@override
String toString() => '$symbol ($name)';
}
當使用這個類時,例如在下面的語句中,我得到一個“初始化靜態字段時的循環依賴” -錯誤。
Currency currency = Currency.EURO;
誰能向我解釋發生了什么?
我無法重現您的錯誤,但是在您將其他人重定向到的構造const
之前缺少一個const
const Currency(this.symbol, this.name);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.