{"id":44080,"date":"2025-06-17T01:42:38","date_gmt":"2025-06-16T17:42:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44080.html"},"modified":"2025-06-17T01:42:38","modified_gmt":"2025-06-16T17:42:38","slug":"flutter%e4%b9%8bgetx%e6%a1%86%e6%9e%b6%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44080.html","title":{"rendered":"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u524d\u8a00<\/li>\n<li>GetX\u4f7f\u7528\u5efa\u8bae<\/li>\n<li>\u72b6\u6001\u7ba1\u7406<\/li>\n<li>GetX\u5feb\u901f\u4e0a\u624b<\/li>\n<li>GetX\u57fa\u672c\u529f\u80fd\u4ecb\u7ecd<\/li>\n<li>\n<ul>\n<li>**\u6838\u5fc3\u4f5c\u7528**<\/li>\n<li>**\u4ee3\u7801\u793a\u4f8b**<\/li>\n<li>**\u5173\u952e\u7ec6\u8282**<\/li>\n<li>**\u6027\u80fd\u5efa\u8bae**<\/li>\n<\/ul>\n<\/li>\n<li>\u53c2\u8003\u94fe\u63a5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u524d\u8a00<\/h3>\n<p>\u5728Reddit\u4e0a&#xff0c;\u8bdf\u75c5GetX\u7684\u58f0\u97f3\u5f88\u591a&#xff0c;\u4e3b\u8981\u662f\u8bf4\u5b83\u505a\u7684\u4e8b\u60c5\u592a\u591a&#xff0c;\u4e0d\u662f\u5355\u4e00\u529f\u80fd\u7ec4\u4ef6&#xff0c;\u8fdd\u53cd\u5355\u4e00\u804c\u8d23\u539f\u5219\u3002\u5bb9\u6613\u88ab\u6ee5\u7528&#xff0c;\u5bb9\u6613\u4ea7\u751f\u610f\u5927\u5229\u9762\u5f0f\u7684\u4ee3\u7801\u7b49\u7b49\u3002\u4ed6\u4eec\u63a8\u8350riverpod\u3002GetX\u4e0a\u4e00\u6b21\u7248\u672c\u66f4\u65b0\u65f62021\u5e74\u4e86\u3002<\/p>\n<p>\u5b83\u60f3\u505a\u7684\u592a\u591a\u4e86&#xff1a; \u4e00\u4e2a\u8bd5\u56fe\u505a\u6240\u6709\u4e8b\u60c5\u7684\u5e93&#xff0c;\u867d\u7136\u65b9\u4fbf&#xff0c;\u4f46\u5e76\u4e0d\u80fd\u7ed9\u4f60\u67b6\u6784\u5e26\u6765\u591a\u5c11\u7075\u6d3b\u6027\u3002\u5982\u679cGetX\u7a81\u7136\u5728\u4f60\u67d0\u4e2aDI\u7528\u4f8b\u6216\u8def\u7531\u7528\u4f8b\u4e2d\u5931\u8d25\u4e86&#xff0c;\u4f60\u5c31\u4f1a\u7a81\u7136\u9047\u5230\u5e93\u81a8\u80c0\u7684\u95ee\u9898&#xff0c;\u8fd9\u5f88\u5bb9\u6613\u5bfc\u81f4\u5de5\u7a0b\u5e08\u4eec\u7528\u201c\u5404\u79cd\u98ce\u683c\u201d\u6765\u5199\u89e3\u51b3\u65b9\u6848\u3002\u6700\u7ec8\u5bfc\u81f4\u610f\u5927\u5229\u9762\u4ee3\u7801\u548c\u96be\u4ee5\u8ffd\u8e2a\u7684bug\u3002<\/p>\n<p>\u5b83\u504f\u79bb\u4e86Flutter\u7684Context\u6a21\u578b&#xff1a; \u56e0\u4e3aGetX\u7684\u201c\u5b9a\u4f4d\u5668\u201d\u662f\u9759\u6001\u53ef\u7528\u7684&#xff0c;\u4e0d\u4f9d\u8d56\u4e8eBuildContext&#xff0c;\u6240\u4ee5\u5b83\u4eec\u672c\u8d28\u4e0a\u5f88\u8106\u5f31&#xff0c;\u5e76\u4e14\u8fdd\u80cc\u4e86Flutter\u6700\u521d\u7684\u8bbe\u8ba1\u539f\u5219\u3002\u50cf\u6e32\u67d3\u751f\u547d\u5468\u671f\u8fd9\u6837\u7684\u4e1c\u897f&#xff0c;\u5e76\u6ca1\u6709\u50cfProvider\u6216Riverpod\u90a3\u6837\u88ab\u9075\u5b88\u3002<\/p>\n<p>\u6211\u7528\u8fc7provider\u3001GetX\u548cRiverpod&#xff0c;\u800c\u4e14\u5230\u76ee\u524d\u4e3a\u6b62&#xff0c;Riverpod\u662f\u72b6\u6001\u7ba1\u7406\u4e2d\u6700\u5bb9\u6613\u6d4b\u8bd5\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u628a\u5b83\u548cGet\u7ed3\u5408\u8d77\u6765&#xff0c;\u4f60\u5c31\u80fd\u5f97\u5230\u4f60\u6240\u9700\u8981\u7684\u4e00\u5207&#xff0c;\u6211\u89c9\u5f97\u3002<\/p>\n<p>Provider\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u72b6\u6001\u7ba1\u7406\u6846\u67b6&#xff0c;\u5b83\u57fa\u4e8eInheritedWidget\u548cChangeNotifier\u5b9e\u73b0&#xff0c;\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5171\u4eab\u72b6\u6001&#xff0c;\u5e76\u652f\u6301\u4f9d\u8d56\u6ce8\u5165&#xff0c;\u975e\u5e38\u9002\u5408\u5c0f\u578b\u5e94\u7528\u548c\u521d\u5b66\u8005\u3002<\/p>\n<p>Riverpod\u4f5c\u4e3aProvider\u7684\u5347\u7ea7\u7248&#xff0c;\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd&#xff0c;\u540c\u65f6\u4fdd\u6301\u4e86\u7b80\u6d01\u6027\u548c\u6613\u7528\u6027\u3002\u5b83\u4f7f\u7528\u5168\u65b0\u7684\u67b6\u6784&#xff0c;\u5177\u5907\u51fa\u8272\u7684\u4f9d\u8d56\u7ba1\u7406\u548c\u5f02\u6b65\u5904\u7406\u80fd\u529b&#xff0c;\u9002\u5408\u4e2d\u5927\u578b\u5e94\u7528\u548c\u6709\u4e00\u5b9a\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u3002<\/p>\n<p>GetX\u5219\u662f\u4e00\u4e2a\u5feb\u901f\u3001\u8f7b\u91cf\u7ea7\u7684\u72b6\u6001\u7ba1\u7406\u548c\u8def\u7531\u7ba1\u7406\u5e93&#xff0c;\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u5229\u7684\u529f\u80fd&#xff0c;\u5982\u4f9d\u8d56\u6ce8\u5165\u3001\u8def\u7531\u7ba1\u7406\u3001\u56fd\u9645\u5316\u3001\u4e3b\u9898\u5207\u6362\u7b49\u3002GetX\u7684\u8bed\u6cd5\u7b80\u6d01\u4e14\u6027\u80fd\u4f18\u79c0&#xff0c;\u975e\u5e38\u9002\u5408\u6784\u5efa\u4e2d\u5c0f\u578b\u5e94\u7528&#xff0c;\u80fd\u591f\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n<p>\u6b64\u5916&#xff0c;Redux\u3001MobX\u548cBLoC\u4e5f\u662fFlutter\u4e2d\u5e38\u89c1\u7684\u72b6\u6001\u7ba1\u7406\u6846\u67b6\u3002Redux\u901a\u8fc7\u5355\u4e00\u7684\u72b6\u6001\u5b58\u50a8\u5e93\u7ba1\u7406\u72b6\u6001&#xff0c;MobX\u57fa\u4e8e\u54cd\u5e94\u5f0f\u7f16\u7a0b&#xff0c;\u800cBLoC\u5219\u57fa\u4e8eReactive Programming\u548cStream\u3002<\/p>\n<h3>GetX\u4f7f\u7528\u5efa\u8bae<\/h3>\n<p>\u5728\u67d0\u4e9b\u5730\u65b9&#xff0c;GetX \u975e\u5e38\u7075\u6d3b&#xff08;\u592a\u7075\u6d3b\u4e86&#xff09;&#xff0c;\u800c\u4e14\u5bb9\u6613\u88ab\u8bef\u7528\u3002\u5982\u679c\u4f60\u6ca1\u6709\u597d\u597d\u8003\u8651\u5982\u4f55\u6b63\u786e\u4f7f\u7528\u5b83&#xff0c;\u4f60\u4f1a\u9047\u5230\u5f88\u591a\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u51e0\u6761\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7684\u89c4\u5219&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8ba9\u4f60\u7684\u6a21\u578b\u548c\u670d\u52a1\u5c42\u72ec\u7acb\u4e8e GetX\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u90a3\u4e2a\u90e8\u5206\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165&#xff0c;\u4f7f\u7528\u5176\u4ed6\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ea\u5728\u8868\u73b0\u5c42\u4f7f\u7528 GetX \u7684\u53ef\u89c2\u5bdf\u5bf9\u8c61\u548c\u4f9d\u8d56\u7ba1\u7406&#xff08;\u4f8b\u5982 Get.put, Get.find&#xff09;\u3002\u8868\u73b0\u5c42\u662f\u9875\u9762\u63a7\u5236\u5668\u548c\u5c0f\u90e8\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ea\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528 GetX \u7684\u5bfc\u822a\u51fd\u6570&#xff08;\u4f8b\u5982 Get.to, Get.back&#xff09;\u3002\u907f\u514d\u5728\u5c0f\u90e8\u4ef6\u4e2d\u4f7f\u7528\u5b83\u4eec&#xff0c;\u5c24\u5176\u662f\u5728\u4f60\u7684\u6a21\u578b\u548c\u670d\u52a1\u5c42\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>GetX \u5305\u542b\u5f88\u591a\u5feb\u6377\u51fd\u6570&#xff0c;\u6bd4\u5982 Get.height&#xff0c;\u4e0d\u8981\u4f7f\u7528\u5b83\u4eec&#xff0c;\u4f7f\u7528\u6807\u51c6\u7684 Flutter \u7b49\u6548\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b MediaQuery.of(context).size.height<\/p>\n<\/li>\n<li>\n<p>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 GetX \u7684\u5b9e\u7528\u7a0b\u5e8f\u6765\u663e\u793a\u5bf9\u8bdd\u6846\u3001\u63d0\u793a\u6761\u548c\u5e95\u90e8\u5f39\u51fa\u6846\u3002\u4f46\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u5b83\u4eec&#xff0c;\u53ea\u80fd\u4ece\u63a7\u5236\u5668\u4e2d\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f46\u662f&#xff0c;\u4f60\u4e5f\u6709\u5f88\u5927\u51e0\u7387\u4f1a\u9047\u5230 GetX \u8def\u7531\u7684\u95ee\u9898\u3002\u5b83\u4e0d\u591f\u7075\u6d3b&#xff0c;\u800c\u4e14\u6709\u70b9 bug\u3002\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u53e6\u4e00\u4e2a\u8def\u7531\u5668&#xff0c;\u4f60\u9700\u8981\u5f88\u597d\u5730\u7406\u89e3 GetX \u7684\u5de5\u4f5c\u539f\u7406\u3002\u56e0\u4e3a\u6807\u51c6\u8def\u7531\u5668\u7ba1\u7406\u63a7\u5236\u5668\u7684\u751f\u547d\u5468\u671f\u3002\u4f60\u9700\u8981\u7528\u5916\u90e8\u8def\u7531\u5668\u5b9e\u73b0\u7c7b\u4f3c\u7684\u4e1c\u897f\u3002<\/p>\n<\/li>\n<li>\n<p>GetX \u548c Riverpod \u53ef\u4ee5\u65e0\u7f1d\u6df7\u5408\u4f7f\u7528&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002\u6df7\u5408\u4f7f\u7528\u8fd9\u4e24\u79cd\u6846\u67b6\u7684\u5173\u952e\u5728\u4e8e\u804c\u8d23\u5206\u79bb&#xff0c;\u8ba9\u6bcf\u4e2a\u6846\u67b6\u5904\u7406\u81ea\u5df1\u64c5\u957f\u7684\u90e8\u5206\u3002\u901a\u8fc7 GetX \u6765\u5904\u7406 UI \u5c42\u7684\u72b6\u6001\u548c\u8def\u7531&#xff0c;\u540c\u65f6\u4f7f\u7528 Riverpod \u6765\u7ba1\u7406\u590d\u6742\u7684\u5168\u5c40\u72b6\u6001\u6216\u4e1a\u52a1\u903b\u8f91&#xff0c;\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e24\u8005\u7684\u4f18\u52bf\u3002\u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u7b80\u6d01\u7684 UI \u5c42\u7ba1\u7406\u65b9\u6848\u5e76\u4e14\u540c\u65f6\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u65f6&#xff0c;\u6df7\u5408\u4f7f\u7528\u8fd9\u4e24\u8005\u662f\u4e00\u79cd\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728 GetX \u72b6\u6001\u7ba1\u7406\u6846\u67b6\u4e2d&#xff0c;\u4f7f\u7528 Obx \u5305\u88f9\u4e00\u4e2a\u7ec4\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u662f \u81ea\u52a8\u76d1\u542c\u54cd\u5e94\u5f0f\u53d8\u91cf&#xff08;Rx&#xff09;\u7684\u53d8\u5316&#xff0c;\u5e76\u5728\u53d8\u91cf\u503c\u66f4\u65b0\u65f6\u81ea\u52a8\u91cd\u5efa\u8be5\u7ec4\u4ef6&#xff0c;\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u5c40\u90e8\u5237\u65b0&#xff0c;\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40 build\u3002<\/p>\n<hr \/>\n<h3>\u72b6\u6001\u7ba1\u7406<\/h3>\n<p>\u4e0d\u7528\u72b6\u6001\u7ba1\u7406\u6846\u67b6\u65f6&#xff0c;\u76f4\u63a5\u5728Statefulwidget\u4e2d\u5b9a\u4e49\u72b6\u6001\u7c7b&#xff0c;\u91cc\u9762\u6301\u6709\u6240\u6709\u6570\u636e&#xff0c;\u7136\u540e\u5728\u4e8b\u4ef6\u5904\u7406\u4e2d\u8c03\u7528HTTP\u8bf7\u6c42\u83b7\u53d6\u6570\u636e&#xff0c;\u5728setState()\u5237\u65b0\u754c\u9762\u3002\u4f46\u8fd9\u79cd\u65b9\u5f0f\u5728\u8de8\u7ec4\u4ef6\u5171\u4eab\u72b6\u6001\u65f6\u5c31\u4e0d\u597d\u5904\u7406\u4e86\u3002\u4e8e\u662f\u6709\u4e86\u51e0\u5927\u72b6\u6001\u7ba1\u7406\u6846\u67b6\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250616174230-685057861e5d9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u518d\u6446\u4e00\u4e2aInheritedWidget\u7684\u6d41\u7a0b\u56fe\u5427&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250616174232-685057886345d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> InheritedWidget\u5bf9\u5b50\u8282\u70b9\u7684Element&#xff0c;\u6709\u4e2a\u5f3a\u5927\u7684\u64cd\u4f5c\u529f\u80fd&#xff1a;\u53ef\u4ee5\u5c06\u5b50widget\u7684element\u5b9e\u4f8b&#xff0c;\u50a8\u5b58\u5728\u81ea\u8eab\u7684InheritedElement\u4e2d\u7684_dependents\u53d8\u91cf\u4e2d\u8c03\u7528\u5176notifyClients\u65b9\u6cd5&#xff0c;\u4f1a\u904d\u5386_dependents\u4e2d\u7684\u5b50Element&#xff0c;\u7136\u540e\u8c03\u7528\u5b50Element\u7684markNeedsBuild\u65b9\u6cd5&#xff0c;\u5c31\u5b8c\u6210\u4e86\u5b9a\u70b9\u5237\u65b0\u5b50\u8282\u70b9\u7684\u64cd\u4f5c\u3002<\/p>\n<h3>GetX\u5feb\u901f\u4e0a\u624b<\/h3>\n<ul>\n<li>\u4ee3\u7801\u62c6\u5206\u4e3aView, Controller\u548cHttp\u8bf7\u6c42\u4e09\u5927\u90e8\u5206&#xff0c;\u5f53\u7136\u8fd8\u6709model<\/li>\n<li>Controller\u7edf\u4e00\u7ba1\u7406\u6240\u6709\u6570\u636e&#xff0c;\u8d1f\u8d23Http\u4ea4\u4e92<\/li>\n<li>view\u53ea\u7ba1\u6e32\u67d3Rx\u6570\u636e&#xff0c;\u7528Obx\u5305\u88f9\u9700\u8981\u5237\u65b0\u7684\u5c0f\u90e8\u4ef6&#xff0c;\u6ce8\u610f\u7c92\u5ea6\u5c3d\u53ef\u80fd\u5c0f<\/li>\n<li>\u6709\u51e0\u4e2aHttp\u8bf7\u6c42&#xff0c;\u5c31\u5728controller\u4e2d\u5b9a\u4e49\u51e0\u4e2aFuture&#xff0c;\u901a\u8fc7FutureBuilder\u6765\u89e6\u53d1Http\u8bf7\u6c42&#xff0c;\u4e0d\u8981\u81ea\u5df1\u5728initState()\u663e\u793a\u8c03\u7528<\/li>\n<\/ul>\n<h3>GetX\u57fa\u672c\u529f\u80fd\u4ecb\u7ecd<\/h3>\n<h4>\u6838\u5fc3\u4f5c\u7528<\/h4>\n<li>\n<p>\u81ea\u52a8\u8ba2\u9605\u54cd\u5e94\u5f0f\u53d8\u91cf<\/p>\n<ul>\n<li>Obx \u5185\u90e8\u4f1a\u76d1\u542c\u5176\u56de\u8c03\u51fd\u6570&#xff08;() &#061;&gt; Widget&#xff09;\u4e2d\u4f7f\u7528\u7684\u6240\u6709 Rx \u53d8\u91cf&#xff08;\u5982 RxInt\u3001RxString\u3001RxList \u7b49&#xff09;\u3002<\/li>\n<li>\u5f53\u8fd9\u4e9b\u53d8\u91cf\u53d1\u751f\u53d8\u5316\u65f6&#xff0c;Obx \u4f1a\u81ea\u52a8\u89e6\u53d1\u5305\u88f9\u7684\u7ec4\u4ef6\u91cd\u5efa\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5c40\u90e8\u5237\u65b0&#xff0c;\u6027\u80fd\u4f18\u5316<\/p>\n<ul>\n<li>\u4e0d\u540c\u4e8e setState \u4f1a\u91cd\u5efa\u6574\u4e2a\u9875\u9762&#xff0c;Obx \u53ea\u66f4\u65b0\u5176\u5305\u88f9\u7684\u7ec4\u4ef6&#xff0c;\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6e32\u67d3\u5f00\u9500\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7b80\u6d01\u7684\u8bed\u6cd5<\/p>\n<ul>\n<li>\u65e0\u9700\u624b\u52a8\u7ba1\u7406\u8ba2\u9605\u6216\u53d6\u6d88\u8ba2\u9605&#xff08;\u76f8\u6bd4 GetBuilder \u6216 StreamBuilder&#xff0c;\u4ee3\u7801\u66f4\u7b80\u6d01&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">CounterController<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">GetxController<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">var<\/span> count <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0.<\/span>obs<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u58f0\u660e\u54cd\u5e94\u5f0f\u53d8\u91cf<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">MyPage<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">StatelessWidget<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">final<\/span> controller <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Get<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">put<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">CounterController<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6ce8\u5165\u63a7\u5236\u5668<\/span><\/p>\n<p>  <span class=\"token metadata function\">&#064;override<\/span><br \/>\n  <span class=\"token class-name\">Widget<\/span> <span class=\"token function\">build<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">BuildContext<\/span> context<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Scaffold<\/span><span class=\"token punctuation\">(<\/span><br \/>\n      body<span class=\"token punctuation\">:<\/span> <span class=\"token class-name\">Center<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        child<span class=\"token punctuation\">:<\/span> <span class=\"token class-name\">Obx<\/span><span class=\"token punctuation\">(<\/span><br \/>\n          <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token class-name\">Text<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">&#034;Count: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">${<\/span><span class=\"token expression\">controller<span class=\"token punctuation\">.<\/span>count<span class=\"token punctuation\">.<\/span>value<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u81ea\u52a8\u76d1\u542c count \u53d8\u5316<\/span><br \/>\n        <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n      <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n      floatingActionButton<span class=\"token punctuation\">:<\/span> <span class=\"token class-name\">FloatingActionButton<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        onPressed<span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span><span class=\"token operator\">&gt;<\/span> controller<span class=\"token punctuation\">.<\/span>count<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u4fee\u6539 count \u4f1a\u89e6\u53d1 Obx \u66f4\u65b0<\/span><br \/>\n        child<span class=\"token punctuation\">:<\/span> <span class=\"token class-name\">Icon<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Icons<\/span><span class=\"token punctuation\">.<\/span>add<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n      <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<hr \/>\n<h4>\u5173\u952e\u7ec6\u8282<\/h4>\n<li>\n<p>\u4f9d\u8d56\u7684\u53d8\u91cf\u5fc5\u987b\u4e3a Rx \u7c7b\u578b<\/p>\n<ul>\n<li>Obx \u4ec5\u5bf9 .obs \u521b\u5efa\u7684\u54cd\u5e94\u5f0f\u53d8\u91cf\u751f\u6548&#xff08;\u5982 0.obs\u3001&#034;hello&#034;.obs&#xff09;&#xff0c;\u666e\u901a\u53d8\u91cf\u65e0\u6548\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f5c\u7528\u57df\u9650\u5236<\/p>\n<ul>\n<li>Obx \u53ea\u4f1a\u76d1\u542c\u5176\u56de\u8c03\u51fd\u6570\u5185\u90e8\u76f4\u63a5\u4f7f\u7528\u7684 Rx \u53d8\u91cf\u3002\u4f8b\u5982&#xff1a;<span class=\"token class-name\">Obx<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token class-name\">Text<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">${<\/span><span class=\"token expression\">controller<span class=\"token punctuation\">.<\/span>count<span class=\"token punctuation\">.<\/span>value<\/span><span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6709\u6548<\/span><br \/>\n<span class=\"token comment\">\/\/ \u9519\u8bef&#xff01;\u4e0b\u9762\u7684\u5199\u6cd5\u4e0d\u4f1a\u76d1\u542c count&#xff1a;<\/span><br \/>\n<span class=\"token comment\">\/\/ var text &#061; &#034;Count: ${controller.count.value}&#034;;<\/span><br \/>\n<span class=\"token comment\">\/\/ Obx(() &#061;&gt; Text(text));<\/span>\n <\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0e GetBuilder \u7684\u533a\u522b<\/p>\n<ul>\n<li>Obx \u9002\u7528\u4e8e\u54cd\u5e94\u5f0f\u53d8\u91cf&#xff08;Rx&#xff09;&#xff0c;\u81ea\u52a8\u8ba2\u9605\u53d8\u5316\u3002<\/li>\n<li>GetBuilder \u9700\u8981\u624b\u52a8\u8c03\u7528 update()&#xff0c;\u9002\u5408\u975e\u54cd\u5e94\u5f0f\u72b6\u6001\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>\u6027\u80fd\u5efa\u8bae<\/h4>\n<ul>\n<li>\u907f\u514d\u8fc7\u5ea6\u5d4c\u5957 Obx&#xff1a;\u6bcf\u4e2a Obx \u4f1a\u72ec\u7acb\u8ba2\u9605\u53d8\u91cf&#xff0c;\u5d4c\u5957\u8fc7\u591a\u53ef\u80fd\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li>\u590d\u6742\u573a\u666f\u4f7f\u7528 GetX \u6216 GetBuilder&#xff1a;\u5982\u679c\u9700\u8981\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236&#xff08;\u5982\u9632\u6296\u3001\u6761\u4ef6\u66f4\u65b0&#xff09;&#xff0c;\u53ef\u4ee5\u8003\u8651\u5176\u4ed6 GetX \u5de5\u5177\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7 Obx&#xff0c;GetX \u5b9e\u73b0\u4e86\u7c7b\u4f3c Flutter \u539f\u751f ValueListenableBuilder \u6216 StreamBuilder \u7684\u529f\u80fd&#xff0c;\u4f46\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u6027\u80fd\u66f4\u4f18\u3002<\/p>\n<h3>\u53c2\u8003\u94fe\u63a5<\/h3>\n<ul>\n<li>get_it: \u4e0d\u662fGetX,\u636e\u8bf4\u548criverpod\u642d\u914d\u8d77\u6765\u5f88\u597d\u7528<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb876\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf12\u6b21\u3002GetX\u4f7f\u7528\u57fa\u7840<\/p>\n","protected":false},"author":2,"featured_media":44078,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2878,4096],"topic":[],"class_list":["post-44080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-flutter","tag-getx"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/44080.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb876\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf12\u6b21\u3002GetX\u4f7f\u7528\u57fa\u7840\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44080.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-16T17:42:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250616174230-685057861e5d9.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44080.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44080.html\",\"name\":\"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-16T17:42:38+00:00\",\"dateModified\":\"2025-06-16T17:42:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44080.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44080.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44080.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/44080.html","og_locale":"zh_CN","og_type":"article","og_title":"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb876\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf12\u6b21\u3002GetX\u4f7f\u7528\u57fa\u7840","og_url":"https:\/\/www.wsisp.com\/helps\/44080.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-16T17:42:38+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250616174230-685057861e5d9.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44080.html","url":"https:\/\/www.wsisp.com\/helps\/44080.html","name":"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-16T17:42:38+00:00","dateModified":"2025-06-16T17:42:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44080.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44080.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44080.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Flutter\u4e4bGetX\u6846\u67b6\u7684\u4f7f\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=44080"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44078"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44080"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}