如何将For In循环变量插入到变量调用中

How to Insert For In Loop Variable into Variable Call

    class GraphData {
    List <double> closingPricesBTC = [];
    List <double> closingPricesETH = [];
    List <double> closingPricesLTC = [];

        for (String cryptoCurrency in cryptoAbbreviation){
        .
        . (some code)
        .
        double closePrice = ....

        closingPrices.add(closePrice);
        }

}

cryptoAbbreviation 在哪里

const List<String> cryptoAbbreviation = ['BTC', 'ETH', 'LTC'];

如何将当前的“cryptocurrency”字符串附加到 closingPrices.add(closePrice) 的变量名称中,以便我可以得到 closingPricesBTC.add(closePrice)closingPricesETH.add(closePrice) 和 [=15] =]

我试过了closingPrices$cryptoCurrency.add(closePrice);,但没用。

你不能生成变量名,但你可以试试 HashMap(Java)/Map(Dart)。这应该可以满足您的要求。

Map<String, List<double>> closingPrices = {'BTC':[], 'ETH':[], 'LTC':[]};


        for (String cryptoCurrency in cryptoAbbreviation){
        .
        . (some code)
        .
        double closePrice = ....

        closingPrices[cryptoCurrency].add(closePrice);
        }