Kenneth Saey
Kenneth Saey

Reputation: 157

Dart - Circular dependency while initializing static field

I'm new to dart and just encountered an issue which I don't understand yet. I wrote this class:

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

When using this class, for example with the statement below I get a "Circular dependency while initializing static field"-error.

Currency currency = Currency.EURO;

Could anyone explain to me what is going on?

Upvotes: 1

Views: 909

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657771

I can't reproduce your error, but a const was missing before the constructor you redirect others to

const Currency(this.symbol, this.name);

Upvotes: 2

Related Questions