【问题标题】:Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ReferralPolicyResponse?' Flutter未处理的异常:类型“_InternalLinkedHashMap<String, dynamic>”不是类型“ReferralPolicyResponse?”的子类型扑
【发布时间】:2022-01-19 10:42:05
【问题描述】:

当我尝试从 json 中的数据将数据分配给 ReferralPolicyResponse 类型的对象时,出现此错误。我得到了回应,但 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;
  }

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

【问题讨论】:

    标签: android ios flutter dart cross-platform


    【解决方案1】:

    我假设refPol 的类型是ReferralPolicyResponse.fromJson

    试试这个

      fetchInitData() async {
        setState(() {
          isBusy=true;
        });
        ApiService _apiService = ApiService();
        BaseData? result = await _apiService.getReferralPolicy();
        if (result?.data != null) {
          refPol = ReferralPolicyResponse.fromJson(Map.from(result?.data));
          print(refPol?.data?.id);
          setState(() {
            showData=true;
          });
        }
        else {
          setState(() {
            showData=false;
          });
        }
        setState(() {
          isBusy=false;
        });
      }
    

    【讨论】:

    • Map.fromJson 部分出错,FromJson 方法未定义。
    • 嘿,我编辑了答案 - 应该是 Map.from
    猜你喜欢
    • 2021-09-13
    • 2022-11-30
    • 2020-08-20
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多