问题描述
class GraphData {
List <double> closingPricesBTC = [];
List <double> closingPricesETH = [];
List <double> closingPricesLTC = [];
for (String cryptoCurrency in cryptoAbbreviation){
.
. (some code)
.
double closePrice = ....
closingPrices.add(closePrice);
}
}
cryptoAbreviation的缩写
const List<String> cryptoAbbreviation = ['BTC','ETH','LTC'];
如何将当前的“ cryptocurrency”字符串附加到closingPrices.add(closePrice)
的变量名称中,以便以closingPricesBTC.add(closePrice)
,closingPricesETH.add(closePrice)
和closingPricesLTC.add(closePrice)
结尾
我尝试过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);
}