{"id":337,"date":"2024-05-09T11:09:02","date_gmt":"2024-05-09T03:09:02","guid":{"rendered":"https:\/\/www.ixriver.com\/?p=337"},"modified":"2024-05-09T14:13:10","modified_gmt":"2024-05-09T06:13:10","slug":"dart-%e5%8f%af%e8%bf%ad%e4%bb%a3%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.ai.ixriver.com\/?p=337","title":{"rendered":"Dart \u53ef\u8fed\u4ee3\u96c6\u5408"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E5%85%83%E7%B4%A0%E8%AF%BB%E5%8F%96\" title=\"\u5143\u7d20\u8bfb\u53d6\">\u5143\u7d20\u8bfb\u53d6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%85%83%E7%B4%A0%E9%81%8D%E5%8E%86\" title=\"\u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386\">\u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E8%AE%BF%E9%97%AE%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0\" title=\"\u793a\u4f8b\uff1a\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\">\u793a\u4f8b\uff1a\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8firstWhere\" title=\"\u793a\u4f8b\uff1a\u4f7f\u7528firstWhere()\">\u793a\u4f8b\uff1a\u4f7f\u7528firstWhere()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E6%9D%A1%E4%BB%B6%E6%A3%80%E6%9F%A5\" title=\"\u6761\u4ef6\u68c0\u67e5\">\u6761\u4ef6\u68c0\u67e5<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8any%E5%92%8Cevery\" title=\"\u793a\u4f8b\uff1a\u4f7f\u7528any()\u548cevery()\">\u793a\u4f8b\uff1a\u4f7f\u7528any()\u548cevery()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E8%BF%87%E6%BB%A4\" title=\"\u8fc7\u6ee4\">\u8fc7\u6ee4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.ai.ixriver.com\/?p=337\/#%E6%98%A0%E5%B0%84\" title=\"\u6620\u5c04\">\u6620\u5c04<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%85%83%E7%B4%A0%E8%AF%BB%E5%8F%96\"><\/span>\u5143\u7d20\u8bfb\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%85%83%E7%B4%A0%E9%81%8D%E5%8E%86\"><\/span>\u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  Iterable&lt;int> iterable = &#91;1, 2, 3];\n  for (final e in iterable) {\n    print(e);\n  }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A%E8%AE%BF%E9%97%AE%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0\"><\/span>\u793a\u4f8b\uff1a\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  Iterable&lt;int> iterable = &#91;1, 2, 3];\n  print('${iterable.first} ${iterable.last}');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8firstWhere\"><\/span>\u793a\u4f8b\uff1a\u4f7f\u7528firstWhere()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>firstWhere()<\/code>\u67e5\u627e\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u6b64\u65b9\u6cd5\u8981\u6c42\u60a8\u4f20\u9012\u4e00\u4e2a<em>\u8c13\u8bcd<\/em>\uff0c\u8be5\u8c13\u8bcd\u662f\u4e00\u4e2a\u5982\u679c\u8f93\u5165\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u5219\u8fd4\u56de true \u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iterable&lt;int> iterable = &#91;1, 2, 3];  \nfinal p = iterable.firstWhere((element) => element == 2);\nprint(p);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%9D%A1%E4%BB%B6%E6%A3%80%E6%9F%A5\"><\/span>\u6761\u4ef6\u68c0\u67e5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u8be5\u7c7bIterable\u63d0\u4f9b\u4e86\u4e24\u79cd\u53ef\u7528\u4e8e\u9a8c\u8bc1\u6761\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>any()\uff1a\u5982\u679c\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u8fd4\u56de true<br>every()\uff1a\u5982\u679c\u6240\u6709\u5143\u7d20\u90fd\u6ee1\u8db3\u6761\u4ef6\uff0c\u5219\u8fd4\u56de true\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%BD%BF%E7%94%A8any%E5%92%8Cevery\"><\/span>\u793a\u4f8b\uff1a\u4f7f\u7528any()\u548cevery()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  const items = &#91;'Salad', 'Popcorn', 'Toast'];\n\n  if (items.any((item) => item.contains('a'))) {\n    print('At least one item contains \"a\"');\n  }\n\n  if (items.every((item) => item.length >= 5)) {\n    print('All items have length >= 5');\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%BF%87%E6%BB%A4\"><\/span>\u8fc7\u6ee4<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul>\n<li>firstWhere(): \u67e5\u627e\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<\/li>\n\n\n\n<li>singleWhere(): \u67e5\u627e\u552f\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u6216\u8005\u5b58\u5728\u591a\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u76f4\u63a5\u629b\u51faStateError\u5f02\u5e38<\/li>\n\n\n\n<li>where(): \u67e5\u627e\u6ee1\u8db3\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20<\/li>\n\n\n\n<li>takeWhile(): \u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u4e4b\u524d\u7684\u6240\u6709\u5143\u7d20<\/li>\n\n\n\n<li>skipWhile(): \u8df3\u8fc7\u6ee1\u8db3\u6761\u4ef6\u4e4b\u524d\u7684\u6240\u6709\u5143\u7d20<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {\n  const numbers = &#91;1, 3, -2, 0, 4, 5];\n\n  var numbersUntilZero = numbers.takeWhile((number) => number != 0);\n  print('Numbers until 0: $numbersUntilZero');\n  \/\/ \u8f93\u51fa\uff1a Numbers until 0: (1, 3, -2)\n\n\n  var numbersStartingAtZero = numbers.skipWhile((number) => number != 0);\n  print('Numbers starting at 0: $numbersStartingAtZero');\n  \/\/ \u8f93\u51fa\uff1aNumbers starting at 0: (0, 4, 5)\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%98%A0%E5%B0%84\"><\/span>\u6620\u5c04<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>map()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u6539\u53d8\u8fed\u4ee3\u91cc\u9762\u7684\u6bcf\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u4e58\u4ee510\u7684\u5217\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iterable&lt;int> output = numbers.map((number) => number * 10);<\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\uff0c\u628a\u8fed\u4ee3\u5143\u7d20\u7c7b\u578b\u4eceint\u8f6c\u6362\u4e3aString\uff0c\u4e5f\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5176\u5b83\u81ea\u5b9a\u4e49\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iterable&lt;int> iterable = &#91;1, 2, 3];\nfinal itString = iterable.map((e) => e.toString());\nprint(itString);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5143\u7d20\u8bfb\u53d6 \u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386 \u793a\u4f8b\uff1a\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20 \u793a\u4f8b\uff1a\u4f7f\u7528firstWhere() firstWhe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"blocksy_meta":[],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"post_excerpt_stackable":"<p>\u76ee\u5f55 Toggle \u5143\u7d20\u8bfb\u53d6\u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386\u793a\u4f8b\uff1a\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u793a\u4f8b\uff1a\u4f7f\u7528firstWhere()\u6761\u4ef6\u68c0\u67e5\u793a\u4f8b\uff1a\u4f7f\u7528any()\u548cevery()\u8fc7\u6ee4\u6620\u5c04 \u5143\u7d20\u8bfb\u53d6 \u793a\u4f8b\uff1a\u5143\u7d20\u904d\u5386 Iterabl&hellip;<\/p>\n","category_list":"<a href=\"https:\/\/www.ai.ixriver.com\/?cat=20\" rel=\"category\">Flutter<\/a>","author_info":{"name":"admin","url":"https:\/\/www.ai.ixriver.com\/?author=1"},"comments_num":"0 comments","_links":{"self":[{"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/337"}],"collection":[{"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=337"}],"version-history":[{"count":10,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ai.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}