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;
  int totalPage;

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

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

  Map toJson() {
    final Map data = new Map();
    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 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 toJson() {
    final Map data = new Map();
    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;
  }
}

你可能感兴趣的:(flutter使用记录)