http_result.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class HttpResult<T> {
  2. HttpResult(
  3. {required this.isSuccess,
  4. dynamic dataJson,
  5. List<dynamic>? listJson,
  6. this.code = -1,
  7. this.msg,
  8. this.errorMsg}) {
  9. this._dataJson = dataJson;
  10. this._listJson = listJson;
  11. }
  12. //是否成功
  13. bool isSuccess = false;
  14. //成功的数据(Json数据)
  15. dynamic _dataJson;
  16. List<dynamic>? _listJson;
  17. //成功的数据(真正的数据)
  18. T? data;
  19. List<T>? list;
  20. //当前返回对象的code,目前定义的是 code = 0 是成功
  21. int code = -1;
  22. //成功之后的消息
  23. String? msg;
  24. //失败的数据,失败字符串
  25. String? errorMsg;
  26. /// 以Json对象的方式获取data对象
  27. Map<String, dynamic>? getDataJson() {
  28. if (_dataJson is Map<String, dynamic>) {
  29. return _dataJson as Map<String, dynamic>;
  30. }
  31. return null;
  32. }
  33. /// 以原始对象的方式获取,可以获取到String,Int,bool等基本类型
  34. dynamic getDataDynamic() {
  35. return _dataJson;
  36. }
  37. /// 以数组的方式获取
  38. List<dynamic>? getListJson() {
  39. return _listJson;
  40. }
  41. /// 设置真正的数据对象
  42. void setData(T data) {
  43. this.data = data;
  44. }
  45. void setList(List<T> list) {
  46. this.list = list;
  47. }
  48. /// 基本类型转换为指定的泛型类型
  49. // ignore: avoid_shadowing_type_parameters
  50. HttpResult<T> convert<T>({T? data, List<T>? list}) {
  51. var result = HttpResult<T>(
  52. isSuccess: this.isSuccess,
  53. dataJson: this._dataJson,
  54. listJson: this._listJson,
  55. code: this.code,
  56. msg: this.msg,
  57. errorMsg: this.errorMsg);
  58. result.data = data;
  59. result.list = list;
  60. return result;
  61. }
  62. }