Flutter Exception: type 'double' is not a subtype of type 'String'

数据转换问题 ,假如后台不想改 或者 返回的字段有时是int 有时是double 进行下面bena对象操作

import 'dart:convert';

import 'package:fl_lanhuo/http/http_api_response.dart';

class TripRecordListEntity {
  int currentPage;
  List<Result> result;
  int totalPage;

  static CommonResponse<TripRecordListEntity> parseData(String response) {
    return CommonResponse.fromJson(json.decode(response), (dataJson) {
      return TripRecordListEntity.fromJson(dataJson);
    });
  }

  TripRecordListEntity.fromJson(Map<String, dynamic> json) {
    currentPage = json['currentPage'];
    if (json['result'] != null) {
      result = new List<Result>();
      json['result'].forEach((v) {
        result.add(new Result.fromJson(v));
      });
    }
    totalPage = json['totalPage'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['currentPage'] = this.currentPage;
    if (this.result != null) {
      data['result'] = this.result.map((v) => v.toJson()).toList();
    }
    data['totalPage'] = this.totalPage;
    return data;
  }
}

class Result {
  String date; //1
  String driverDistance; //这里后台可能返回的是double 3.00或者 int 0 导致解析错误
  String driverUserName; //1
  String endDate; //1
  int largeTotal; //1
  int middleTotal; //1
  int orderTotal; //1
  int smallTotal; //1
  String startDate; //1

  Result.fromJson(Map<String, dynamic> json) {
    date = json['date'];
    //进行强制转换成String
    driverDistance = json['driverDistance'].toString();//这里强制转换为String 统一接受即可
    driverUserName = json['driverUserName'];
    endDate = json['endDate'];
    largeTotal = json['largeTotal'];
    middleTotal = json['middleTotal'];
    orderTotal = json['orderTotal'];
    smallTotal = json['smallTotal'];
    startDate = json['startDate'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['date'] = this.date;
    data['driverDistance'] = this.driverDistance;
    data['driverUserName'] = this.driverUserName;
    data['endDate'] = this.endDate;
    data['largeTotal'] = this.largeTotal;
    data['middleTotal'] = this.middleTotal;
    data['orderTotal'] = this.orderTotal;
    data['smallTotal'] = this.smallTotal;
    data['startDate'] = this.startDate;
    return data;
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值