The argument type 'x' can't be assigned to the parameter type 'x'
Seems there are conflicting imports for EntitlementsModel
. Try to rewrite all your imports to be of the form:
import 'package:YOUR_PACKAGE/../...dart'
'YOUR_PACKAGE' should be the name of the application as stated in pubspec.yml name
variable.
And the directory structure all the folders from the lib
folder (not including it) up to the imported dart file.
(you use this import scheme in the second line of your Fifth.dart file)
Related videos on Youtube
Asyraf Dayan
Updated on June 04, 2022Comments
-
Asyraf Dayan almost 2 years
Below is my model class:
crm_dependent_list_model.dart (Model class)
import 'crm_dep_entitlement_model.dart'; class DependantModel{ String name; String relationship; EntitlementsModel entitlements; DependantModel({this.name, this.relationship, this.entitlements}); factory DependantModel.fromJson(Map depjson){ return DependantModel( name: depjson["Name"].toString(), relationship: depjson["Relationship"].toString(), entitlements: EntitlementsModel.fromJson(depjson["Entitlements"]) ); } }
This is the EntitlementsModel located inside DependantModel class
crm_dep_entitlement_model.dart
class EntitlementsModel{ final GP gp; final OPS ops; final IP ip; final Dental dental; final Optical optical; final EHS ehs; EntitlementsModel({this.gp, this.ops, this.ip, this.dental, this.optical, this.ehs}); factory EntitlementsModel.fromJson(Map ejson){ return EntitlementsModel( gp: GP.fromJson(ejson["GP"]), ops: OPS.fromJson(ejson["OPS"]), ip: IP.fromJson(ejson["IP"]), dental: Dental.fromJson(ejson["Dental"]), optical: Optical.fromJson(ejson["Optical"]), ehs: EHS.fromJson(ejson["EHS"]) ); } } //GP class class GP{ final String entitlement, utilisation, balance; GP({this.entitlement, this.utilisation, this.balance}); factory GP.fromJson(Map gjson){ return GP( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } } //OPS class class OPS{ final String entitlement, utilisation, balance; OPS({this.entitlement, this.utilisation, this.balance}); factory OPS.fromJson(Map gjson){ return OPS( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } } //IP class class IP{ final String entitlement, utilisation, balance; IP({this.entitlement, this.utilisation, this.balance}); factory IP.fromJson(Map gjson){ return IP( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } } //Dental class class Dental{ final String entitlement, utilisation, balance; Dental({this.entitlement, this.utilisation, this.balance}); factory Dental.fromJson(Map gjson){ return Dental( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } } //Optical class class Optical{ final String entitlement, utilisation, balance; Optical({this.entitlement, this.utilisation, this.balance}); factory Optical.fromJson(Map gjson){ return Optical( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } } //EHS class class EHS{ final String entitlement, utilisation, balance; EHS({this.entitlement, this.utilisation, this.balance}); factory EHS.fromJson(Map gjson){ return EHS( entitlement: gjson["Entitlement"].toString(), utilisation: gjson["Utilisation"].toString(), balance: gjson["Balance"].toString() ); } }
This model class is currently being used to pull data from JSON in this class:
Fifth.dart (class calling the JSON data)
import 'package:flutter/material.dart'; import 'package:emas_app/Dependant.dart' as Dep; import 'model/crm_dependent_list_model.dart'; import 'dart:convert'; import 'dart:async'; import 'package:http/http.dart' as http; final String url = "http://crm.emastpa.com.my/MemberInfo.json"; //Future to get list of dependent names Future<List<DependantModel>> fetchUserInfo() async{ http.Response response = await http.get(url); var responsejson = json.decode(response.body); return(responsejson[0]['Dependents'] as List) .map((user) => DependantModel.fromJson(user)) .toList(); } class Fifth extends StatefulWidget { @override _FifthState createState() => _FifthState(); } class _FifthState extends State<Fifth> { static Future<List<DependantModel>> depState; @override void initState() { depState = fetchUserInfo(); super.initState(); } @override Widget build(BuildContext context) { //ListView.builder inside FutureBuilder var futureBuilder = new FutureBuilder( future: depState, builder: (context, snapshot){ switch(snapshot.connectionState){ case ConnectionState.none: case ConnectionState.waiting: return new Center( child: new CircularProgressIndicator(), ); default: if(snapshot.hasError){ return new Text(snapshot.error); }else{ List<DependantModel> user = snapshot.data; return new ListView.builder( itemCount: user.length, itemBuilder: (context, index){ return new Column( children: <Widget>[ new ListTile( title: new Text(user[index].name, style: TextStyle(fontSize: 20.0)), subtitle: new Text(user[index].relationship, style: TextStyle(fontSize: 15.0)), trailing: new MaterialButton(color: Colors.greenAccent, textColor: Colors.white, child: new Text("More"), onPressed: (){ Navigator.push(context, new MaterialPageRoute(builder: (context) => Dep.Dependents(name: user[index].name, entitlementsModel: user[index].entitlements)) ); } ), ) ], ); }); } } }); return new Scaffold( body: futureBuilder, ); } }
The Fifth.dart class will send over data through the constructor in this class:
Dependent.dart (class with the constructor)
import 'model/crm_dep_entitlement_model.dart'; import 'package:flutter/material.dart'; import 'dart:convert'; import 'dart:async'; import 'package:http/http.dart' as http; import 'model/crm_dependent_list_model.dart'; import 'package:flutter/foundation.dart'; final String url = "http://crm.emastpa.com.my/MemberInfo.json"; //Future method to fetch information Future<EntitlementsModel> fetchEntitlements() async{ final response = await http.get(url); final jsonresponse = json.decode(response.body); var res = jsonresponse[0]["Dependents"][0]["Entitlements"]; return EntitlementsModel.fromJson(jsonresponse[0]["Dependents"][0]["Entitlements"]); } //void main() => runApp(Dependents()); class Dependents extends StatefulWidget { final String name; // final Map entitlement; final EntitlementsModel entitlementsModel; //Constructor to accept the value from Fifth.dart // Dependents({Key key, this.name, this.dependantModel) : super(key: key); Dependents({Key key, this.name, this.entitlementsModel}) : super(key:key); @override _DependentsState createState() => _DependentsState(); } class _DependentsState extends State<Dependents> { Future<EntitlementsModel> entitlement; @override void initState() { entitlement = fetchEntitlements(); super.initState(); } @override Widget build(BuildContext context) { //new body widget Widget body = new Container( child: new Center( child: new FutureBuilder( future: entitlement, builder: (context, snapshot){ if(snapshot.hasData){ var entitledata = snapshot.data; //retrieve data from snapshot var gpentitlement = entitledata.gp.entitlement; var gputilisation = entitledata.gp.utilisation; var gpbalance = entitledata.gp.balance; var opsentitle = entitledata.ip.entitlement; var opsutilisation = entitledata.ip.utilisation; var opsbalance = entitledata.ip.balance; return new Column( children: <Widget>[ new ListTile( title: new Text("Name: "), subtitle: new Text("${widget.name}"), ) , new Divider( color: Colors.black, ), new ListTile( title: new Text("Clinic GP", style: TextStyle( fontWeight: FontWeight.bold, ), ), ) , new ListTile( title: new Text("Entitlement"), trailing: new Text(gpentitlement), ), new ListTile( title: new Text("Utilisation"), trailing: new Text(gputilisation), ), new ListTile( title: new Text("Balance"), trailing: new Text(gpbalance), ), new Divider( color: Colors.black, ), new ListTile( title: new Text("IP", style: TextStyle( fontWeight: FontWeight.bold, ), ), ), new ListTile( title: new Text("Entitlement"), trailing: new Text(opsentitle), ), new ListTile( title: new Text("Utilisation"), trailing: new Text(opsutilisation), ), new ListTile( title: new Text("Balance"), trailing: new Text(opsbalance), ), ], ); }else if(snapshot.hasError){ return new Text(snapshot.error); } //loading the page return new Center( child: new CircularProgressIndicator(), ); }), ), ); return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('${widget.name}'), ), body: body ), ); } }
This is the error I am running into:
compiler message: lib/Fifth.dart:72:155: Error: The argument type '#lib1::EntitlementsModel' can't be assigned to the parameter type '#lib2::EntitlementsModel'. compiler message: Try changing the type of the parameter, or casting the argument to '#lib2::EntitlementsModel'. compiler message: new MaterialPageRoute(builder: (context) => Dep.Dependents(name: user[index].name, entitlementsModel: user[index].entitlements)) compiler message:
and also:
I/flutter ( 6816): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 6816): The following assertion was thrown building FutureBuilder<List<DependantModel>>(dirty, state: I/flutter ( 6816): _FutureBuilderState<List<DependantModel>>#bdb2c): I/flutter ( 6816): type 'NoSuchMethodError' is not a subtype of type 'String'
My question is:
How do I fix the error because its saying I should cast the argument but I do not know how because the EntitlementsModel is a class containing multiple classes of Maps.
-
Günter Zöchbauer over 5 yearsWithout having a closer look at your code - please ensure you don't have relative imports in
lib/main.dart
and you don't importmain.dart
anywhere. -
Asyraf Dayan over 5 years@GünterZöchbauer yes i already checked and I did not import main.dart anywhere.
-
Günter Zöchbauer over 5 yearsWhat about relative imports in
main.dart
(imports that don't start with'dart:...'
or'package:...'
)
-
-
Asyraf Dayan over 5 yearsI have also tried rewriting the imports to the form you are referring to and it did not solve the problem.
-
chemamolins over 5 yearsHave you done that in all files, not only in Fifth.dar? When you have the error with the same type in #lib1 and #lib2, normally is because it considers the type comes from different libraries and normally the reason is using different importing mechanism.
-
Asyraf Dayan over 5 yearsI have managed to fix the library error. Somehow changing the package import worked after a few tries. Thanks for that.
-
Asyraf Dayan over 5 yearsTry taking a look at my second error that I have posted. What does it mean by FutureBuilder<List<DependantModel>>(dirty, state: I/flutter ( 6816): _FutureBuilderState<List<DependantModel>>#bdb2c): I/flutter ( 6816): type 'NoSuchMethodError' is not a subtype of type 'String'
-
chemamolins over 5 yearsTry to get rid of the
depState
static variable and usefetchUserInfo()
directly in the FutureBuilder.