{"id":83,"date":"2025-06-23T10:47:00","date_gmt":"2025-06-23T02:47:00","guid":{"rendered":"https:\/\/www.bqmz001.top\/?p=83"},"modified":"2025-06-23T10:47:00","modified_gmt":"2025-06-23T02:47:00","slug":"flutter-%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%8f%98%e6%88%90%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.bqmz001.top\/index.php\/2025\/06\/23\/flutter-%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%8f%98%e6%88%90%e5%88%97%e8%a1%a8\/","title":{"rendered":"flutter \u8ba9\u6570\u636e\u53d8\u6210\u5217\u8868~"},"content":{"rendered":"<h2>1\u3001\u9996\u5148\u5c06json\u53d8\u6210\u5b9e\u4f53\u65b9\u4fbf\u8c03\u7528<\/h2>\n<h5>contact_bean.dart<\/h5>\n<pre><code>class ContactBean {\n  int? id;\n  String? firstName;\n  String? lastName;\n  String? phoneNumber;\n  int? age;\n  int? sex;\n  String? address;\n\n  ContactBean(\n      {this.id,\n        this.firstName,\n        this.lastName,\n        this.phoneNumber,\n        this.age,\n        this.sex,\n        this.address});\n\n  ContactBean.fromJson(Map&lt;String, dynamic&gt; json) {\n    id = json[&#039;id&#039;];\n    firstName = json[&#039;firstName&#039;];\n    lastName = json[&#039;lastName&#039;];\n    phoneNumber = json[&#039;phoneNumber&#039;];\n    age = json[&#039;age&#039;];\n    sex = json[&#039;sex&#039;];\n    address = json[&#039;address&#039;];\n  }\n\n  Map&lt;String, dynamic&gt; toJson() {\n    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();\n    data[&#039;id&#039;] = this.id;\n    data[&#039;firstName&#039;] = this.firstName;\n    data[&#039;lastName&#039;] = this.lastName;\n    data[&#039;phoneNumber&#039;] = this.phoneNumber;\n    data[&#039;age&#039;] = this.age;\n    data[&#039;sex&#039;] = this.sex;\n    data[&#039;address&#039;] = this.address;\n    return data;\n  }\n}\n<\/code><\/pre>\n<h2>2\u3001\u4fee\u6539\u4e00\u4e0bmain_provider.dart\u8ba9\u8fd4\u56de\u6765\u7684json\u53d8\u6210\u5b9e\u4f53<\/h2>\n<p>\u5148\u8d34\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<h5>main_provider.dart<\/h5>\n<pre><code>import &#039;dart:convert&#039;;\n\nimport &#039;package:day03\/bean\/base_bean.dart&#039;;\nimport &#039;package:day03\/bean\/contact_bean.dart&#039;;\nimport &#039;package:day03\/dio_util.dart&#039;;\nimport &#039;package:dio\/dio.dart&#039;;\nimport &#039;package:flutter\/cupertino.dart&#039;;\n\nclass MainProvider with ChangeNotifier {\n  String _text = &#039;&#039;;\n\n  String get text =&gt; _text;\n\n  List&lt;ContactBean&gt; _dataList = [];\n  List&lt;ContactBean&gt; get dataList =&gt; _dataList;\n\n  void getList() async {\n    await DioUtil.getInstance().get(\n      &quot;http:\/\/192.168.5.116:20080\/contact\/list&quot;,\n      null,\n      () {\n        _text = &quot;loading&quot;;\n        notifyListeners();\n      },\n      (data) {\n        BaseBean bean = BaseBean.fromJson(data);\n        _dataList=List.from(bean.data.map((e) =&gt; ContactBean.fromJson(e)));\n        notifyListeners();\n      },\n      (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n\n  void addContact() async {\n    DioUtil.getInstance().addHeader(&quot;token&quot;, &quot;123456789&quot;);\n    await DioUtil.getInstance().post(\n      &quot;http:\/\/192.168.5.116:20080\/contact\/add&quot;,\n      {\n        &#039;firstName&#039;: &#039;\u738b&#039;,\n        &#039;lastName&#039;: &#039;\u516d&#039;,\n        &#039;phoneNumber&#039;: &#039;13456789010&#039;,\n        &#039;age&#039;: &#039;22&#039;,\n        &#039;sex&#039;: 1,\n        &#039;address&#039;: &#039;\u5c71\u897f\u7701\u664b\u57ce\u5e02&#039;,\n      },\n      () {\n        _text = &quot;loading&quot;;\n        notifyListeners();\n      },\n      (data) {\n        _text = &#039;\u6dfb\u52a0\u6210\u529f&#039;;\n        notifyListeners();\n      },\n      (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n}\n<\/code><\/pre>\n<hr>\n<p>\u9996\u5148\u6dfb\u52a0\u4e86\u4e2adataList\u7684\u53d8\u91cf<\/p>\n<pre><code>  List&lt;ContactBean&gt; _dataList = []; \/\/\u79c1\u6709\u53d8\u91cf\n  List&lt;ContactBean&gt; get dataList =&gt; _dataList; \/\/\u5bf9\u5916\u5f15\u7528\u53ea\u8bfb\n<\/code><\/pre>\n<p>\u7136\u540e\u5728getList()\u65b9\u6cd5\u91cc\u5c06\u539f\u6765\u7684\uff1a<\/p>\n<pre><code>_text = bean.data.toString();\n<\/code><\/pre>\n<p>\u6362\u4e3a\uff1a<\/p>\n<pre><code> _dataList=List.from(bean.data.map((e) =&gt; ContactBean.fromJson(e)));\n<\/code><\/pre>\n<p>\u5c06\u6570\u636e\u66f4\u65b0\u4ece\u66f4\u65b0text\u6587\u5b57\u53d8\u6210dataList\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528map\u65b9\u6cd5\u904d\u5386\u51faContactBean<\/p>\n<p>\u597d\u4e86\uff0c\u6570\u636e\u600e\u4e48\u6765\u89e3\u51b3\u4e86\uff0c\u63a5\u4e0b\u6765\u6539UI<\/p>\n<h2>4\u3001\u6539UI\uff01<\/h2>\n<h5>main.dart<\/h5>\n<pre><code>import &#039;package:day03\/main_provider.dart&#039;;\nimport &#039;package:flutter\/material.dart&#039;;\nimport &#039;package:provider\/provider.dart&#039;;\n\nvoid main() {\n  runApp(\n    MultiProvider(\n      providers: [ChangeNotifierProvider(create: (context) =&gt; MainProvider())],\n      child: MyApp(),\n    ),\n  );\n}\n\nclass MyApp extends StatelessWidget {\n  const MyApp({super.key});\n\n  \/\/ This widget is the root of your application.\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: &#039;Flutter Demo&#039;,\n      theme: ThemeData(\n        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),\n      ),\n      home: const MyHomePage(title: &#039;Flutter Demo Home Page&#039;),\n    );\n  }\n}\n\nclass MyHomePage extends StatelessWidget {\n  const MyHomePage({super.key, required this.title});\n\n  final String title;\n\n  @override\n  Widget build(BuildContext context) {\n    final provider = Provider.of&lt;MainProvider&gt;(context);\n\n    return Scaffold(\n      appBar: AppBar(\n        backgroundColor: Theme.of(context).colorScheme.inversePrimary,\n        title: Text(title),\n      ),\n      body: Center(\n        child: Column(\n          mainAxisAlignment: MainAxisAlignment.center,\n          children: &lt;Widget&gt;[\n            Text(provider.text),\n            ElevatedButton(\n              onPressed: provider.getList,\n              child: const Text(&#039;\u8bf7\u6c42\u6570\u636e&#039;),\n            ),\n            ElevatedButton(\n              onPressed: () =&gt; provider.addContact(),\n              child: const Text(&#039;\u6dfb\u52a0\u4e00\u4e2a\u8054\u7cfb\u4eba&#039;),\n            ),\n            Expanded(\n              child: ListView.builder(\n                itemCount: provider.dataList.length,\n                itemBuilder: (context, index) {\n                  return ListTile(\n                    title: Text(&#039;${provider.dataList[index].firstName} ${provider.dataList[index].lastName}&#039;),\n                    subtitle: Text(&#039;${provider.dataList[index].phoneNumber}&#039;),\n                  );\n                },\n              ),\n            )\n          ],\n        ),\n      ),\n    );\n  }\n}\n<\/code><\/pre>\n<hr>\n<p>\u4ecd\u7136\u662fStatelessWidget\uff0c\u4e0d\u8fc7\u540e\u9762\u7528Expanded\u6491\u5f00\u5269\u4e0b\u7684\u5730\u65b9\uff0c\u5f80\u91cc\u9762\u653e\u4e86\u4e00\u4e2aListView\uff0c\u4f7f\u7528ListView.Builder\u6784\u5efa\u3002<br \/>itemCount\u6307List\u7684\u8ba1\u6570\uff1bitemBuilder\u5185\u653e\u7f6eListView item\u7ec4\u4ef6<br \/>\u4e3a\u6f14\u793a\u65b9\u4fbf\u5c31\u8fd4\u56de\u4e00\u4e2aListTile\u7528\u4e8e\u5c55\u793a\u6570\u636e\uff0c\u76f4\u63a5provider.dataList[index]\u5f15\u7528\u5217\u8868\u91cc\u7684ContactBean\u5bf9\u8c61<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u9996\u5148\u5c06json\u53d8\u6210\u5b9e\u4f53\u65b9\u4fbf\u8c03\u7528 contact_bean.dart class ContactBean { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-androidflutter"],"_links":{"self":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":15,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/83\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}