简体   繁体   English

未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是“ReferralPolicyResponse”类型的子类型? Flutter</string,>

[英]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.

相关问题 未处理的异常:[type '_InternalLinkedHashMap<string, dynamic> ' 不是 'String' 类型的子类型]</string,> - Unhandled exception: [type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'] 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型转换 int flutter 中类型 'OriginDestination' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter 未处理的异常:类型 '_InternalLinkedHashMap<dynamic, dynamic> ' 不是类型 'Map 的子类型<string, dynamic> '?</string,></dynamic,> - Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'? 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'Iterable 的子类型<dynamic> '为什么会出现这个错误</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' why this error coming 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter 未处理的异常:输入“未来”<dynamic> ' 不是 dart/flutter 中“ProductModel”类型的子类型</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'ProductModel' in dart/flutter 未处理的异常:类型&#39;String&#39;不是颤振中&#39;index&#39;的&#39;int&#39;类型的子类型 - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' in flutter Flutter 上的 json 解析错误。 输入&#39;_InternalLinkedHashMap<String, dynamic> &#39; 不是类型转换中类型 &#39;ResultData&#39; 的子类型 - Error in json parsing on flutter. type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ResultData' in type cast 输入'_InternalLinkedHashMap<string, dynamic> ' 不是类型转换中类型 'Session' 的子类型</string,> - type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Session' in type cast _InternalLinkedHashMap <String, dynamic> &#39;不是&#39;List类型的子类型 <Map<dynamic, dynamic> &gt;” - _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM