Limit list data in dart.

huangapple go评论137阅读模式
英文:

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" ? "&#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>';
                    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 &lt; Map &lt; String, dynamic &gt;&gt; areaOfInspec) {
    try {
        if (areaOfInspec != null) {
            var areaOfInspecHtml = &quot;&quot;;
            for (var entry in areaOfInspec) {
                if (entry.length &lt;= 8) {
                    areaOfInspecHtml += &#39;&lt;tr style=&quot;height: 35px;&quot;&gt;&#39;;
                    areaOfInspecHtml +=
                        &#39;&lt;td align=&quot;center&quot; style=&quot;font-family: arial; color: #414141;font-size: 12px;border-right: solid 0.7px #d6d5d5;&quot;&gt;&#39; +
                        entry[&#39;key&#39;] +
                        &#39;&lt;/td&gt;&#39;;

                    areaOfInspecHtml += &#39;&lt;td align=&quot;center&quot; style=&quot;font-family: arial; color: #414141;font-size: 12px;&quot;&gt;&#39;;
                    areaOfInspecHtml += entry[&#39;value&#39;].split(&quot;#&quot;)[0] == &quot;true&quot; ? &quot;&amp;#10003;&quot; : &quot;&amp;#8211;&quot;;
                    areaOfInspecHtml += &#39;&lt;/td&gt;&#39;;

                    areaOfInspecHtml += &#39;&lt;td align=&quot;center&quot; style=&quot;font-family: arial; color: #414141;font-size: 12px;&quot;&gt;&#39;;
                    areaOfInspecHtml += entry[&#39;value&#39;].split(&quot;#&quot;)[1] == &quot;true&quot; ? &quot;&amp;#10003;&quot; : &quot;&amp;#8211;&quot;;
                    areaOfInspecHtml += &#39;&lt;/td&gt;&#39;;

                    areaOfInspecHtml += &#39;&lt;td align=&quot;center&quot; style=&quot;font-family: arial; color: #414141;font-size: 12px;&quot;&gt;&#39;;
                    areaOfInspecHtml += entry[&#39;value&#39;].split(&quot;#&quot;)[2] == &quot;true&quot; ? &quot;&amp;#10003;&quot; : &quot;&amp;#8211;&quot;;
                    areaOfInspecHtml += &#39;&lt;/td&gt;&#39;;

                    areaOfInspecHtml += &#39;&lt;td align=&quot;center&quot; style=&quot;font-family: arial; color: #414141;font-size: 12px;&quot;&gt;&#39;;
                    areaOfInspecHtml += entry[&#39;value&#39;].split(&quot;#&quot;)[3] == &quot;true&quot; ? &quot;&amp;#10003;&quot; : &quot;&amp;#8211;&quot;;
                    areaOfInspecHtml += &#39;&lt;/td&gt;&#39;;
                    areaOfInspecHtml += &quot;&lt;/tr&gt;&quot;;
                }
            }
            return areaOfInspectionHtml;
        } else {
            log(&quot;====*****areaOfInspecHtml not found&quot;);
            return &#39;&#39;;
        }
    } catch (e) {
        throw Exception(&quot;Error&quot; + 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)) {
  ...
}

huangapple
  • 本文由 发表于 2023年4月4日 12:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定