gpt4 book ai didi

android - 如何在Dart中组合循环结果

转载 作者:行者123 更新时间:2023-12-03 04:24:25 26 4
gpt4 key购买 nike

您好,我有一个简单的for循环,我想将循环结果合并为1个用逗号分隔的字符串

   String loc;
for(var q=0; q<2; q++){
userData = res;
loc = userData[q]['location'];
print(loc); //value in first loop is location A value in first second loop is location B i want to combine them on 1 string
}

最佳答案

如果只想在String中追加

void main() {
String loc="";
List<String> locs = ["a","d","B","C"];
for(var q=0; q<2; q++){
loc = (loc.length>0? (loc+", ") : loc)+ (locs[q] ?? " ");
}

print(loc);
}

关于android - 如何在Dart中组合循环结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60539577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com