参数类型“x”无法分配给参数类型“x”

Asy*_*yan 2 json dart flutter

下面是我的模型类:

\n\n

crm_dependent_list_model.dart(模型类)

\n\n
import \'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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是位于DependantModel 类内的EntitlementsModel

\n\n

crm_dep_entitlement_model.dart

\n\n
class 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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

该模型类当前用于从该类中的 JSON 中提取数据:

\n\n

Fifth.dart(调用JSON数据的类)

\n\n
import \'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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

Fifth.dart类将通过此类中构造函数发送数据:

\n\n

Dependent.dart(带有构造函数的类)

\n\n
import \'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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我遇到的错误:

\n\n
compiler 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:\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且:

\n\n
I/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\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的问题是:

\n\n

我如何修复该错误,因为它说我应该转换参数,但我不知道如何解决,因为 EntitlementsModel一个包含多个地图类的类。

\n