[英]Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ReferralPolicyResponse?' Flutter
I am getting this error, when I am trying to assign data to an object of type ReferralPolicyResponse
from data from json.当我尝试从 json 的数据中将数据分配给类型为
ReferralPolicyResponse
的 object 时,出现此错误。 I am getting the response, but varibale is not taking values.我得到了回应,但 varibale 没有取值。 I created fromJson method to assign values from json to variable, but this exception occurs.
我创建了 fromJson 方法来将 json 中的值分配给变量,但是会发生此异常。 Please help me with this.
请帮我解决一下这个。
The code where i am using a variable to store data from json file is (though I am getting response as I want in result.data, I checked while debugging) -我使用变量存储来自 json 文件的数据的代码是(虽然我在 result.data 中得到了我想要的响应,但我在调试时检查了) -
@override
void initState() {
super.initState();
fetchInitData();
}
fetchInitData() async {
setState(() {
isBusy=true;
});
ApiService _apiService = ApiService();
BaseData? result = await _apiService.getReferralPolicy();
if (result?.data != null) {
refPol = result?.data;
print(refPol?.data?.id);
setState(() {
showData=true;
});
}
else {
setState(() {
showData=false;
});
}
setState(() {
isBusy=false;
});
}
getReferralPolicy method - getReferralPolicy 方法 -
Future<BaseData?> getReferralPolicy() async {
Response<BaseData> response = await _apiService
.handleGet(type: FetchDataType.REFERRAL);
return response.data;
}
referral_policy_response.dart - refer_policy_response.dart -
import 'dart:convert';
ReferralPolicyResponse spinResultResponseFromJson(String str) =>
ReferralPolicyResponse.fromJson(json.decode(str));
String spinResultResponseToJson(ReferralPolicyResponse data) =>
json.encode(data.toJson());
class ReferralPolicyResponse {
ReferralPolicyResponse({
this.data,
this.success,
this.message,
});
Data? data;
bool? success;
String? message;
factory ReferralPolicyResponse.fromJson(Map<String, dynamic> json) =>
ReferralPolicyResponse(
data: json["data"] == null ? null : Data.fromJson(json["data"]),
success: json["success"] == null ? null : json["success"],
message: json["message"] == null ? null : json["message"],
);
Map<String, dynamic> toJson() => {
"data": data == null ? null : data?.toJson(),
"success": success == null ? null : success,
"message": message == null ? null : message,
};
}
class Data {
Data({
this.id,
this.onetimeCb,
this.discPercent,
this.lifeTimeCb,
this.cbExpiry,
this.bankTransferable,
});
int? id;
double? onetimeCb;
double? discPercent;
double? lifeTimeCb;
int? cbExpiry;
bool? bankTransferable=false;
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json["id"] == null ? null : json["id"],
onetimeCb: json["onetimeCb"] == null ? null : json["onetimeCb"],
discPercent: json["discPercent"] == null ? null : json["discPercent"],
lifeTimeCb: json["lifeTimeCb"] == null ? null : json["lifeTimeCb"],
cbExpiry: json["cbExpiry"] == null ? null : json["cbExpiry"],
bankTransferable: json["bankTransferable"] == null ? null : json["bankTransferable"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"onetimeCb": onetimeCb == null ? null : onetimeCb,
"discPercent": discPercent == null ? null : discPercent,
"lifeTimeCb": lifeTimeCb == null ? null : lifeTimeCb,
"cbExpiry": cbExpiry == null ? null : cbExpiry,
"bankTransferable": bankTransferable == null ? null : bankTransferable,
};
}
I'm assuming refPol
is of type ReferralPolicyResponse.fromJson
我假设
refPol
的类型是ReferralPolicyResponse.fromJson
Try this尝试这个
fetchInitData() async {
setState(() {
isBusy=true;
});
ApiService _apiService = ApiService();
BaseData? result = await _apiService.getReferralPolicy();
if (result?.data != null) {
refPol = ReferralPolicyResponse.fromJson(Map.fromJson(result?.data));
print(refPol?.data?.id);
setState(() {
showData=true;
});
}
else {
setState(() {
showData=false;
});
}
setState(() {
isBusy=false;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.