{"id":589,"date":"2023-02-18T18:38:38","date_gmt":"2023-02-18T10:38:38","guid":{"rendered":"http:\/\/gjweb.top\/?p=589"},"modified":"2023-02-18T18:38:41","modified_gmt":"2023-02-18T10:38:41","slug":"16-flutter%e5%bc%82%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=589","title":{"rendered":"16. Flutter\u5f02\u6b65"},"content":{"rendered":"\n<p>Dart \u8bed\u8a00\u548c JS \u4e00\u6837\u90fd\u662f\u5355\u7ebf\u7a0b + \u4e8b\u4ef6\u5faa\u73af\u6a21\u5f0f, \u6267\u884c\u65f6\u673a\u4e5f\u548cJS\u7684 event Loop \u4e00\u81f4<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u8fd4\u56de-\u5f02\u6b65\u7684-future\">\u8fd4\u56de \u5f02\u6b65\u7684 future<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Future &lt;String&gt; xxx(<br>    return Future((){<br> &nbsp; &nbsp; &nbsp; &nbsp;return \"xxx\"<br> &nbsp;  })<br>)<br>xxx() async{<br> &nbsp; &nbsp;xxx().then((value){print(value)})<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8fd4\u56de\u6307\u5b9a\u7684-future\">\u8fd4\u56de\u6307\u5b9a\u7684 Future<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Future &lt;String&gt; xxx{<br> &nbsp; &nbsp;return Future.value(\"hello\")<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8fd4\u56de\u5ef6\u65f6\u7684-future\">\u8fd4\u56de\u5ef6\u65f6\u7684 Future<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return Future.delayed(Duration(seconds:3),(){<br> &nbsp; &nbsp;return \"Hello\"<br>})<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u76d1\u542c\u5f02\u5e38\">\u76d1\u542c\u5f02\u5e38<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>.catchError((err){}) \/\/ \u9519\u8bef\u7684\u56de\u8c03<br>.whenComPlete((){}) \/\/ \u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u4f1a\u89e6\u53d1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"async-await\">async &amp; await<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>xxx() async{<br> &nbsp; &nbsp;var res = await xxx()<br> &nbsp; &nbsp;print(res)<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"futurebuilder\">FutureBuilder<\/h2>\n\n\n\n<p>\u81ea\u52a8\u8ffd\u8e2a Future \u7684\u72b6\u6001\u4fe1\u606f,\u4f1a\u81ea\u52a8\u91cd\u7ed8\u7ec4\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Future &lt;String&gt; loadData()async{<br> &nbsp; &nbsp;await Future.delayed(const Duration(secods:2));\/\/ \u7b49\u5f852\u79d2<br> &nbsp; &nbsp;return \"this is data\"<br>}<br>\u200b<br>FutureBuild(<br>    future:loadData(),<br> &nbsp; &nbsp;builder:(BUildContext context,AsyncSnapshot&lt;String&gt; snapshot){<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u68c0\u67e5 ConnectionState \u662f\u5426\u672adone<br> &nbsp; &nbsp; &nbsp; &nbsp;if(snapshot.connectionState == ConnectionState.done){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (snapshot.hasError) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Center(<br>                    child: Text(\"ERROR: ${snapshot.error}\"),<br>                );<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Center(child: Text(\"DATA: ${snapshot.data}\"));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return const Center(<br>                child: CircularProgressIndicator(),<br>            );<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dart \u8bed\u8a00\u548c JS \u4e00\u6837\u90fd\u662f\u5355\u7ebf\u7a0b + \u4e8b\u4ef6\u5faa\u73af\u6a21\u5f0f, \u6267\u884c\u65f6\u673a\u4e5f\u548cJS\u7684 event Loop \u4e00\u81f4 \u8fd4 [&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-589","post","type-post","status-publish","format-standard","hentry","category-flutter"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/589","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=589"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}