英文:
Limit list data in dart
问题
I want to limit list in for in loop. if data list more than 8. It just display 8 list. I've add condition like this if (entry.length <= 8) but it not working.
poweredareaOfInspec(List<Map<String, dynamic>> areaOfInspec) {
try {
if (areaOfInspec != null) {
var areaOfInspecHtml = "";
var count = 0; // Add a count variable to limit to 8 entries
for (var entry in areaOfInspec) {
if (count < 8) { // Check the count
areaOfInspecHtml += '<tr style="height: 35px">';
areaOfInspecHtml +=
'<td align="center" style="font-family: arial; color: #414141;font-size: 12px;border-right: solid 0.7px #d6d5d5">' +
entry['key'] +
'</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px">';
areaOfInspecHtml += entry['value'].split("#")[0] == "true" ? "✓" : "–";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px">';
areaOfInspecHtml += entry['value'].split("#")[1] == "true" ? "✓" : "–";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px">';
areaOfInspecHtml += entry['value'].split("#")[2] == "true" ? "✓" : "–";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px">';
areaOfInspecHtml += entry['value'].split("#")[3] == "true" ? "✓" : "–";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '</tr>';
count++; // Increment count
}
}
return areaOfInspecHtml;
} else {
log("====*****areaOfInspecHtml not found");
return '';
}
} catch (e) {
throw Exception("Error" + e.toString());
}
}
英文:
I want to limit list in for in loop. if data list more than 8. It just display 8 list. I've add condition like this if (entry.length <= 8) but it not working.
poweredareaOfInspec(List < Map < String, dynamic >> areaOfInspec) {
try {
if (areaOfInspec != null) {
var areaOfInspecHtml = "";
for (var entry in areaOfInspec) {
if (entry.length <= 8) {
areaOfInspecHtml += '<tr style="height: 35px;">';
areaOfInspecHtml +=
'<td align="center" style="font-family: arial; color: #414141;font-size: 12px;border-right: solid 0.7px #d6d5d5;">' +
entry['key'] +
'</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px;">';
areaOfInspecHtml += entry['value'].split("#")[0] == "true" ? "&#10003;" : "&#8211;";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px;">';
areaOfInspecHtml += entry['value'].split("#")[1] == "true" ? "&#10003;" : "&#8211;";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px;">';
areaOfInspecHtml += entry['value'].split("#")[2] == "true" ? "&#10003;" : "&#8211;";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += '<td align="center" style="font-family: arial; color: #414141;font-size: 12px;">';
areaOfInspecHtml += entry['value'].split("#")[3] == "true" ? "&#10003;" : "&#8211;";
areaOfInspecHtml += '</td>';
areaOfInspecHtml += "</tr>";
}
}
return areaOfInspectionHtml;
} else {
log("====*****areaOfInspecHtml not found");
return '';
}
} catch (e) {
throw Exception("Error" + e.toString());
}
}
答案1
得分: 3
使用 take
。它将返回列表中的前 N 个项目(如果列表的项目少于 N 个,则返回所有项目):
for (var entry in areaOfInspec.take(8)) {
...
}
英文:
Use take
. It will return the first N items in a list (or all of them if the list has fewer than N items):
for (var entry in areaOfInspec.take(8)) {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论