我一直在使用Retrofit进行API调用.在其中一个API中,发送两个不同的响应并try 在Map<String, dynamic>中获取它们,但在生成的文件中显示错误.

enter image description here

当我将响应类型作为Map&lt;字符串时,其中一个响应的字符串就可以了,但在其他响应中有不同类型的键.当type为Map<String, String>并且Json键具有int值时,捕获到异常.

一种回应

   {
  "count": 1,
  "login_type": "PASSWORD",
  "success": true,
  "type": "object",
  "data": {
    "token_type": "Bearer",
    "expires_in": 1200,
    "access_token": "1o+BYL3LWRs+JpHMK3E5",
    "refresh_token": "OAPwbwFORpQ0R6sg",
    "userId": 358
  }
}

其他回应

{"count":1,"type":"Success","Data":{"Success":TRUE,"otpEnabled":TRUE,"Message":"动态口令已发送到您的手机号码62*32","Timestamp":"","OTP":572800}}

误差率

类型‘int’不是类型转换中的类型‘字符串’的子类型

推荐答案

用户界面:

    Row(
      children: <Widget>[
        ElevatedButton(
          onPressed: firstAPI,
          child: const Text("Call First"),
        ),
        ElevatedButton(
          onPressed: secondAPI,
          child: const Text("Call Second API"),
        ),
      ],
    ),
  ),

第一个API调用(一个响应):

  Future<void> firstAPI() async {
    const String path = "https://www.jsonkeeper.com/b/99ID"; // hosted One res
    final Response<dynamic> res = await Dio().get(path);
    final FirstModel firstModel = FirstModel.fromJson(res.data);
    log("firstAPI(): ${firstModel.toJson()}");
    return Future<void>.value();
  }

第二次接口调用(其他响应):

  Future<void> secondAPI() async {
    const String path = "https://www.jsonkeeper.com/b/49KW"; // hosted Other res
    final Response<dynamic> res = await Dio().get(path);
    final SecondModel secondModel = SecondModel.fromJson(res.data);
    log("secondAPI(): ${secondModel.toJson()}");
    return Future<void>.value();
  }

First_mod.dart:

class FirstModel {
  FirstModel({this.count, this.loginType, this.success, this.type, this.data});

  FirstModel.fromJson(Map<String, dynamic> json) {
    count = json["count"];
    loginType = json["login_type"];
    success = json["success"];
    type = json["type"];
    data = json["data"] != null ? Data.fromJson(json["data"]) : null;
  }
  int? count;
  String? loginType;
  bool? success;
  String? type;
  Data? data;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["count"] = count;
    data["login_type"] = loginType;
    data["success"] = success;
    data["type"] = type;
    if (this.data != null) {
      data["data"] = this.data!.toJson();
    }
    return data;
  }
}

class Data {
  Data({
    this.tokenType,
    this.expiresIn,
    this.accessToken,
    this.refreshToken,
    this.userId,
  });

  Data.fromJson(Map<String, dynamic> json) {
    tokenType = json["token_type"];
    expiresIn = json["expires_in"];
    accessToken = json["access_token"];
    refreshToken = json["refresh_token"];
    userId = json["userId"];
  }
  String? tokenType;
  int? expiresIn;
  String? accessToken;
  String? refreshToken;
  int? userId;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["token_type"] = tokenType;
    data["expires_in"] = expiresIn;
    data["access_token"] = accessToken;
    data["refresh_token"] = refreshToken;
    data["userId"] = userId;
    return data;
  }
}

加入时间:清华2007年01月25日下午3:33

class SecondModel {
  SecondModel({this.count, this.type, this.data});

  SecondModel.fromJson(Map<String, dynamic> json) {
    count = json["count"];
    type = json["type"];
    data = json["data"] != null ? Data.fromJson(json["data"]) : null;
  }
  int? count;
  String? type;
  Data? data;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["count"] = count;
    data["type"] = type;
    if (this.data != null) {
      data["data"] = this.data!.toJson();
    }
    return data;
  }
}

class Data {
  Data({this.success, this.otpEnabled, this.message, this.timestamp, this.otp});

  Data.fromJson(Map<String, dynamic> json) {
    success = json["success"];
    otpEnabled = json["otpEnabled"];
    message = json["message"];
    timestamp = json["timestamp"];
    otp = json["otp"];
  }
  bool? success;
  bool? otpEnabled;
  String? message;
  String? timestamp;
  int? otp;

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["success"] = success;
    data["otpEnabled"] = otpEnabled;
    data["message"] = message;
    data["timestamp"] = timestamp;
    data["otp"] = otp;
    return data;
  }
}

输出:

output

Flutter相关问答推荐

在Flutter 中压缩图片返回空

MobX Build_Runner问题:不生成.G文件(Flutter )

为什么Riverpod生成器在这种情况下不生成AsyncNotiator?

BlocBuilder仅生成一次,并在后续发出时停止重建

悬停时打开Flutter 下拉按钮

Ffmpeg_kit_fltter:^6.0.3版权问题

有没有可能不在BlocBuilder中重新构建内容?

如何在Flutter 安全存储器中存储对象值?

谷歌 map 在 flutter 应用程序中显示错误的当前位置

创建混合 TextWidget 和 TextFieldWidget

FutureProvider 不返回数据给 UI

如果不刷新页面,我看不到从 Firestore 中提取的数据

应用程序和应用程序内的 webview 之间的单点登录

状况不佳 - Cubit

alert 对话框内的 ListView 显示空的透明窗口

Flutter 动画页面计数器文本

如何在 flutter 的alert 对话框中实现进度指示器?

Flutter:制作自定义小部件的最佳做法是什么?

有任何方法可以在应用程序移动背景恢复时显示 appopenAd

Flutter 在气泡聊天消息上对齐时间戳文本