下面是我的模型类:
\n\ncrm_dependent_list_model.dart(模型类)
\n\nimport \'crm_dep_entitlement_model.dart\';\n\nclass DependantModel{\n\n String name;\n String relationship;\n EntitlementsModel entitlements;\n\n DependantModel({this.name, this.relationship, this.entitlements});\n\n factory DependantModel.fromJson(Map depjson){\n\n return DependantModel(\n name: depjson["Name"].toString(),\n relationship: depjson["Relationship"].toString(),\n entitlements: EntitlementsModel.fromJson(depjson["Entitlements"])\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是位于DependantModel 类内的EntitlementsModel
\n\ncrm_dep_entitlement_model.dart
\n\nclass EntitlementsModel{\n\n final GP gp;\n final OPS ops;\n final IP ip;\n final Dental dental;\n final Optical optical;\n final EHS ehs;\n\n EntitlementsModel({this.gp, this.ops, this.ip, this.dental, this.optical, this.ehs});\n\n factory EntitlementsModel.fromJson(Map ejson){\n\n return EntitlementsModel(\n\n gp: GP.fromJson(ejson["GP"]),\n ops: OPS.fromJson(ejson["OPS"]),\n ip: IP.fromJson(ejson["IP"]),\n dental: Dental.fromJson(ejson["Dental"]),\n optical: Optical.fromJson(ejson["Optical"]),\n ehs: EHS.fromJson(ejson["EHS"])\n );\n }\n\n}\n\n\n//GP class\nclass GP{\n\n final String entitlement, utilisation, balance;\n\n GP({this.entitlement, this.utilisation, this.balance});\n\n factory GP.fromJson(Map gjson){\n\n return GP(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\n\n\n//OPS class\nclass OPS{\n\n final String entitlement, utilisation, balance;\n\n OPS({this.entitlement, this.utilisation, this.balance});\n\n factory OPS.fromJson(Map gjson){\n\n return OPS(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\n\n//IP class\nclass IP{\n\n final String entitlement, utilisation, balance;\n\n IP({this.entitlement, this.utilisation, this.balance});\n\n factory IP.fromJson(Map gjson){\n\n return IP(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\n\n\n//Dental class\nclass Dental{\n\n final String entitlement, utilisation, balance;\n\n Dental({this.entitlement, this.utilisation, this.balance});\n\n factory Dental.fromJson(Map gjson){\n\n return Dental(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\n\n\n//Optical class\nclass Optical{\n\n final String entitlement, utilisation, balance;\n\n Optical({this.entitlement, this.utilisation, this.balance});\n\n factory Optical.fromJson(Map gjson){\n\n return Optical(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\n\n\n//EHS class\nclass EHS{\n\n final String entitlement, utilisation, balance;\n\n EHS({this.entitlement, this.utilisation, this.balance});\n\n factory EHS.fromJson(Map gjson){\n\n return EHS(\n entitlement: gjson["Entitlement"].toString(),\n utilisation: gjson["Utilisation"].toString(),\n balance: gjson["Balance"].toString()\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n该模型类当前用于从该类中的 JSON 中提取数据:
\n\nFifth.dart(调用JSON数据的类)
\n\nimport \'package:flutter/material.dart\';\nimport \'package:emas_app/Dependant.dart\' as Dep;\nimport \'model/crm_dependent_list_model.dart\';\nimport \'dart:convert\';\nimport \'dart:async\';\nimport \'package:http/http.dart\' as http;\n\nfinal String url = "http://crm.emastpa.com.my/MemberInfo.json";\n\n//Future to get list of dependent names\nFuture<List<DependantModel>> fetchUserInfo() async{\n\n http.Response response = await http.get(url);\n var responsejson = json.decode(response.body);\n\n return(responsejson[0][\'Dependents\'] as List)\n .map((user) => DependantModel.fromJson(user))\n .toList();\n}\n\nclass Fifth extends StatefulWidget {\n @override\n _FifthState createState() => _FifthState();\n}\n\nclass _FifthState extends State<Fifth> {\n\n static Future<List<DependantModel>> depState;\n\n @override\n void initState() {\n depState = fetchUserInfo();\n super.initState();\n }\n\n @override\n Widget build(BuildContext context) {\n\n //ListView.builder inside FutureBuilder\n var futureBuilder = new FutureBuilder(\n future: depState,\n builder: (context, snapshot){\n switch(snapshot.connectionState){\n case ConnectionState.none:\n case ConnectionState.waiting:\n return new Center(\n child: new CircularProgressIndicator(),\n );\n default:\n if(snapshot.hasError){\n return new Text(snapshot.error);\n }else{\n\n List<DependantModel> user = snapshot.data;\n\n return new ListView.builder(\n itemCount: user.length,\n itemBuilder: (context, index){\n\n return new Column(\n children: <Widget>[\n new ListTile(\n title: new Text(user[index].name,\n style: TextStyle(fontSize: 20.0)),\n subtitle: new Text(user[index].relationship,\n style: TextStyle(fontSize: 15.0)),\n trailing: new MaterialButton(color: Colors.greenAccent,\n textColor: Colors.white,\n child: new Text("More"),\n onPressed: (){\n Navigator.push(context,\n new MaterialPageRoute(builder: (context) => Dep.Dependents(name: user[index].name, entitlementsModel: user[index].entitlements))\n );\n }\n ),\n )\n ],\n );\n });\n }\n }\n });\n\n return new Scaffold(\n body: futureBuilder,\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nFifth.dart类将通过此类中的构造函数发送数据:
\n\nDependent.dart(带有构造函数的类)
\n\nimport \'model/crm_dep_entitlement_model.dart\';\nimport \'package:flutter/material.dart\';\nimport \'dart:convert\';\nimport \'dart:async\';\nimport \'package:http/http.dart\' as http;\nimport \'model/crm_dependent_list_model.dart\';\nimport \'package:flutter/foundation.dart\';\n\nfinal String url = "http://crm.emastpa.com.my/MemberInfo.json";\n\n//Future method to fetch information\nFuture<EntitlementsModel> fetchEntitlements() async{\n\n final response = await http.get(url);\n final jsonresponse = json.decode(response.body);\n\n var res = jsonresponse[0]["Dependents"][0]["Entitlements"];\n\n return EntitlementsModel.fromJson(jsonresponse[0]["Dependents"][0]["Entitlements"]);\n}\n\n//void main() => runApp(Dependents());\nclass Dependents extends StatefulWidget {\n\n final String name;\n// final Map entitlement;\n final EntitlementsModel entitlementsModel;\n\n //Constructor to accept the value from Fifth.dart\n// Dependents({Key key, this.name, this.dependantModel) : super(key: key);\n Dependents({Key key, this.name, this.entitlementsModel}) : super(key:key);\n\n @override\n _DependentsState createState() => _DependentsState();\n}\n\nclass _DependentsState extends State<Dependents> {\n\n Future<EntitlementsModel> entitlement;\n\n @override\n void initState() {\n entitlement = fetchEntitlements();\n super.initState();\n }\n\n @override\n Widget build(BuildContext context) {\n\n //new body widget\n Widget body = new Container(\n child: new Center(\n child: new FutureBuilder(\n future: entitlement,\n builder: (context, snapshot){\n if(snapshot.hasData){\n var entitledata = snapshot.data;\n\n //retrieve data from snapshot\n var gpentitlement = entitledata.gp.entitlement;\n var gputilisation = entitledata.gp.utilisation;\n var gpbalance = entitledata.gp.balance;\n\n var opsentitle = entitledata.ip.entitlement;\n var opsutilisation = entitledata.ip.utilisation;\n var opsbalance = entitledata.ip.balance;\n\n return new Column(\n children: <Widget>[\n new ListTile(\n title: new Text("Name: "),\n subtitle: new Text("${widget.name}"),\n ) ,\n new Divider(\n color: Colors.black,\n ),\n new ListTile(\n title: new Text("Clinic GP",\n style: TextStyle(\n fontWeight: FontWeight.bold,\n ),\n ),\n ) ,\n new ListTile(\n title: new Text("Entitlement"),\n trailing: new Text(gpentitlement),\n ),\n new ListTile(\n title: new Text("Utilisation"),\n trailing: new Text(gputilisation),\n ),\n new ListTile(\n title: new Text("Balance"),\n trailing: new Text(gpbalance),\n ),\n new Divider(\n color: Colors.black,\n ),\n new ListTile(\n title: new Text("IP",\n style: TextStyle(\n fontWeight: FontWeight.bold,\n ),\n ),\n ),\n new ListTile(\n title: new Text("Entitlement"),\n trailing: new Text(opsentitle),\n ),\n new ListTile(\n title: new Text("Utilisation"),\n trailing: new Text(opsutilisation),\n ),\n new ListTile(\n title: new Text("Balance"),\n trailing: new Text(opsbalance),\n ),\n ],\n );\n\n\n }else if(snapshot.hasError){\n return new Text(snapshot.error);\n }\n\n //loading the page\n return new Center(\n child: new CircularProgressIndicator(),\n );\n }),\n ),\n );\n\n return MaterialApp(\n home: Scaffold(\n appBar: AppBar(\n title: Text(\'${widget.name}\'),\n ),\n body: body\n ),\n );\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我遇到的错误:
\n\ncompiler message: lib/Fifth.dart:72:155: Error: The argument type \'#lib1::EntitlementsModel\' can\'t be assigned to the parameter type \'#lib2::EntitlementsModel\'.\ncompiler message: Try changing the type of the parameter, or casting the argument to \'#lib2::EntitlementsModel\'.\ncompiler message: new MaterialPageRoute(builder: (context) => Dep.Dependents(name: user[index].name, entitlementsModel: user[index].entitlements))\ncompiler message:\nRun Code Online (Sandbox Code Playgroud)\n\n并且:
\n\nI/flutter ( 6816): \xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nI/flutter ( 6816): The following assertion was thrown building FutureBuilder<List<DependantModel>>(dirty, state:\nI/flutter ( 6816): _FutureBuilderState<List<DependantModel>>#bdb2c):\nI/flutter ( 6816): type \'NoSuchMethodError\' is not a subtype of type \'String\'\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是:
\n\n我如何修复该错误,因为它说我应该转换参数,但我不知道如何解决,因为 EntitlementsModel是一个包含多个地图类的类。
\n似乎存在冲突的导入EntitlementsModel。尝试将所有导入重写为以下形式:
import 'package:YOUR_PACKAGE/../...dart'
“YOUR_PACKAGE”应该是 pubspec.yml 变量中所述的应用程序名称name。
目录结构包括从lib文件夹(不包括它)到导入的 dart 文件的所有文件夹。
(您在 Fifth.dart 文件的第二行中使用此导入方案)
| 归档时间: |
|
| 查看次数: |
11470 次 |
| 最近记录: |