class HttpResult { HttpResult( {required this.isSuccess, dynamic dataJson, List? listJson, this.code = -1, this.msg, this.errorMsg}) { this._dataJson = dataJson; this._listJson = listJson; } //是否成功 bool isSuccess = false; //成功的数据(Json数据) dynamic _dataJson; List? _listJson; //成功的数据(真正的数据) T? data; List? list; //当前返回对象的code,目前定义的是 code = 0 是成功 int code = -1; //成功之后的消息 String? msg; //失败的数据,失败字符串 String? errorMsg; /// 以Json对象的方式获取data对象 Map? getDataJson() { if (_dataJson is Map) { return _dataJson as Map; } return null; } /// 以原始对象的方式获取,可以获取到String,Int,bool等基本类型 dynamic getDataDynamic() { return _dataJson; } /// 以数组的方式获取 List? getListJson() { return _listJson; } /// 设置真正的数据对象 void setData(T data) { this.data = data; } void setList(List list) { this.list = list; } /// 基本类型转换为指定的泛型类型 // ignore: avoid_shadowing_type_parameters HttpResult convert({T? data, List? list}) { var result = HttpResult( isSuccess: this.isSuccess, dataJson: this._dataJson, listJson: this._listJson, code: this.code, msg: this.msg, errorMsg: this.errorMsg); result.data = data; result.list = list; return result; } }