{"id":591,"date":"2023-02-18T18:39:08","date_gmt":"2023-02-18T10:39:08","guid":{"rendered":"http:\/\/gjweb.top\/?p=591"},"modified":"2023-02-18T18:39:10","modified_gmt":"2023-02-18T10:39:10","slug":"17-stream%e6%b5%81%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=591","title":{"rendered":"17. Stream\u6d41\u64cd\u4f5c"},"content":{"rendered":"\n<p>Steam \u53ef\u4ee5\u672a\u6765\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u6216\u591a\u4e2a\u503c, \u7c7b\u4f3c\u4e8e\u6c34\u6d41\u7684\u903b\u8f91, \u53ea\u8981\u6709\u6c34\u8fc7\u6765\u5c31\u4f1a\u76d1\u542c\u5230; \u540c\u6bd4 Future \u53ea\u4f1a\u5728\u672a\u6765\u83b7\u53d6\u4e00\u4e2a\u503c.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final stream=Stream.periodic(const Duration(seconds: 1),(value)=&gt;value);<br>void initState() {<br>    super.initState();<br>    future.then((value) =&gt; print(value));<br>    stream.listen((event) {<br>        print(\"stream:$event\");<br>    });<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"streambuilder\">streamBuilder<\/h2>\n\n\n\n<p>\u573a\u666f: \u5c06 Stream \u6d41\u4e0e Widget \u7ed3\u5408\u5230\u4e00\u8d77\u7684\u7ec4\u4ef6, \u53ef\u5b9e\u73b0\u7ec4\u4ef6\u7684 <strong>\u5c40\u90e8\u6570\u636e\u66f4\u65b0<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>StreamBuilder(<br>    builder:(context,snapshot){<br> &nbsp; &nbsp; &nbsp; &nbsp;print(snapshot.connectionState)<br> &nbsp;  },<br> &nbsp; &nbsp;stream:streamFun()<br>)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"snapshot-connectionstate\u7684\u72b6\u6001\">snapshot.connectionState\u7684\u72b6\u6001<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>waiting \u7b49\u5f85\u4e2d<\/li><li>active \u83b7\u53d6\u4e2d<\/li><li>none \u6ca1\u6709\u6570\u636e\u6d41<\/li><li>done \u5df2\u5b8c\u6210<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"streamcontroller-\u76d1\u542c\u66f4\u7cbe\u786e\u7684\u6570\u636e\u6d41\">StreamController \u76d1\u542c\u66f4\u7cbe\u786e\u7684\u6570\u636e\u6d41<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:async';<br>\/\/ \u5b9a\u4e49<br>final _controller = StreamController&lt;int&gt;(); <br>\/\/ \u5173\u95ed\u6d41<br>void dispose() {<br>    super.dispose();<br>    _controller.close();<br>}<br>\/\/ \u83b7\u53d6\u6d41<br>streamBuilder(<br>    stream:_controller()\uff0c<br> &nbsp; &nbsp;builder:(context,snapsht){<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp;  }<br>)<br>\u200b<\/code><\/pre>\n\n\n\n<p>\u9ed8\u8ba4 StreamControoler \u53ea\u80fd\u6709\u4e00\u4e2a\u76d1\u542c\u8005,\u5982\u679c\u51fa\u73b0\u591a\u4e2a\u76d1\u542c\u53ef\u4ee5\u7528 StreamController.broadcast() \u521b\u5efa\u6d41<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8fc7\u6ee4\u6d41-stream\">\u8fc7\u6ee4\u6d41 stream<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>StreamBuilder(<br>    stream:_controller.where((value){<br> &nbsp; &nbsp; &nbsp; &nbsp;return value &gt; 2;<br> &nbsp;  })<br>)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Steam \u53ef\u4ee5\u672a\u6765\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u6216\u591a\u4e2a\u503c, \u7c7b\u4f3c\u4e8e\u6c34\u6d41\u7684\u903b\u8f91, \u53ea\u8981\u6709\u6c34\u8fc7\u6765\u5c31\u4f1a\u76d1\u542c\u5230; \u540c\u6bd4 Future  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-flutter"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=591"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/591\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/591\/revisions\/592"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}