簡體   English   中英

未處理的異常:類型 '_InternalLinkedHashMap<string, dynamic> ' 不是“ReferralPolicyResponse”類型的子類型? Flutter</string,>

[英]Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ReferralPolicyResponse?' Flutter

當我嘗試從 json 的數據中將數據分配給類型為ReferralPolicyResponse的 object 時,出現此錯誤。 我得到了回應,但 varibale 沒有取值。 我創建了 fromJson 方法來將 json 中的值分配給變量,但是會發生此異常。 請幫我解決一下這個。

我使用變量存儲來自 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 方法 -

Future<BaseData?> getReferralPolicy() async {
    Response<BaseData> response = await _apiService
        .handleGet(type: FetchDataType.REFERRAL);
    return response.data;
  }

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,
  };
}

我假設refPol的類型是ReferralPolicyResponse.fromJson

嘗試這個

  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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM