使用方法中的对象工厂模式:

使用 factory 关键字标识类的构造函数将会令该构造函数变为工厂构造函数,这将意味着使用该构造函数构造类的实例时并非总是会返回新的实例对象。例如,工厂构造函数可能会从缓存中返回一个实例,或者返回一个子类型的实例。

Use the factory keyword when implementing a constructor that doesn’t always create a new instance of its class. For example, a factory constructor might return an instance from a cache, or it might return an instance of a subtype. Another use case for factory constructors is initializing a final variable using logic that can’t be handled in the initializer list.

数据传入层

  1. Wallet _wallet = Wallet.fromJson(res.data['data']['wallet']);

数据类型定义层 — 使用类的工厂模式

  1. import 'package:json_annotation/json_annotation.dart';
  2. part 'wallet.g.dart';
  3. @JsonSerializable()
  4. class Wallet {
  5. Wallet();
  6. String wallet_id;
  7. String user_id;
  8. num usdt_balance;
  9. num fil_balance;
  10. num lamb_balance;
  11. num user_space_num;
  12. factory Wallet.fromJson(Map<String,dynamic> json) => _$WalletFromJson(json);
  13. Map<String, dynamic> toJson() => _$WalletToJson(this);
  14. }

数据返回层

  1. // GENERATED CODE - DO NOT MODIFY BY HAND
  2. part of 'wallet.dart';
  3. // **************************************************************************
  4. // JsonSerializableGenerator
  5. // **************************************************************************
  6. Wallet _$WalletFromJson(Map<String, dynamic> json) {
  7. return Wallet()
  8. ..wallet_id = json['wallet_id'] as String
  9. ..user_id = json['user_id'] as String
  10. ..usdt_balance = json['usdt_balance'] as num
  11. ..fil_balance = json['fil_balance'] as num
  12. ..lamb_balance = json['lamb_balance'] as num
  13. ..user_space_num = json['user_space_num'] as num;
  14. }
  15. Map<String, dynamic> _$WalletToJson(Wallet instance) => <String, dynamic>{
  16. 'wallet_id': instance.wallet_id,
  17. 'user_id': instance.user_id,
  18. 'usdt_balance': instance.usdt_balance == 0 ? "0.000000 FIL" : instance.usdt_balance ,
  19. 'fil_balance': instance.fil_balance,
  20. 'lamb_balance': instance.lamb_balance,
  21. 'user_space_num': instance.user_space_num
  22. };

image.pngimage.png

子机继承 - 数据处理

子数据

  1. import 'package:nebula_app/models/profitParent.dart';
  2. class ProfitSecond extends ProfitParent{
  3. num todayPersonSpaceEarnings ; // 个人存储空间收益
  4. num todayPromotionRelease ; // 今日推广释放
  5. /// 继承 -- 父级
  6. // num todayProfitToltal; // 今日总收益
  7. // num todayReleaseToltal; // 今日总释放收益
  8. // num todayPledge; // 今日前置质押
  9. // num todayLock; // 今日锁仓
  10. // num todayCompanyPledge; // 今日公司质押
  11. ProfitSecond(Map<String, dynamic> json) : super.init(json){
  12. this.todayPersonSpaceEarnings = json['release_person'];
  13. this.todayPromotionRelease = json['release_manager'];
  14. }
  15. // 获取个人空间收益
  16. get getSpace{
  17. final _data = todayPersonSpaceEarnings == null ? '0.000000 FIL' : todayPersonSpaceEarnings;
  18. print("哦买噶我是个人空间收益 --- $_data");
  19. return _data;
  20. }
  21. // 获取推广收益
  22. get getPromotion{
  23. final _data = todayPromotionRelease == null ? '0.000000 FIL' : todayPromotionRelease;
  24. print("哦买噶我是推广收益 --- $_data");
  25. return _data;
  26. }
  27. // 设置 当前属性的值.
  28. set setPropertyChild(data){
  29. final that = this;
  30. // 数据为 Object
  31. if(data is Map){
  32. final name = data['name'];
  33. // final value = data['value'];
  34. // print(value);
  35. switch (name) {
  36. case "todayPersonSpaceEarnings":
  37. that.todayPersonSpaceEarnings = data['value'];
  38. break;
  39. case "todayPromotionRelease" :
  40. that.todayPromotionRelease = data['value'];
  41. break;
  42. default:
  43. throw 'Object传入的值应该name,value的形式传入...';
  44. }
  45. }
  46. }
  47. /// 设置个人空间数据
  48. // set getSpace(val){
  49. // print('别动修改我好吗? $val');
  50. // this.todayPersonSpaceEarnings = val;
  51. // }
  52. // /// 设置推广收益
  53. // set getPromotion(val){
  54. // this.todayPromotionRelease = val;
  55. // }
  56. }

父数据

  1. class ProfitParent{
  2. num todayProfitToltal; // 今日总收益
  3. num todayReleaseToltal; // 今日总释放收益
  4. num todayPledge; // 今日前置质押
  5. num todayLock; // 今日锁仓
  6. num todayCompanyPledge; // 今日公司质押
  7. ProfitParent.init(data){
  8. this.todayProfitToltal = data['profit_total_today'];
  9. this.todayLock = data['release_total_today'];
  10. this.todayCompanyPledge= data['locked_total_today'];
  11. this.todayPledge = data['today_pledge'];
  12. this.todayReleaseToltal = data['today_company_pledge'];
  13. }
  14. set setPropertyParent(data){
  15. final that = this;
  16. // 数据为 Object
  17. if(data is Map){
  18. final name = data['name'];
  19. switch (name) {
  20. case "todayProfitToltal":
  21. that.todayProfitToltal = data['value'];
  22. break;
  23. case "todayLock" :
  24. that.todayLock = data['value'];
  25. break;
  26. case "todayCompanyPledge" :
  27. that.todayCompanyPledge = data['value'];
  28. break;
  29. case "todayPledge" :
  30. that.todayPledge = data['value'];
  31. break;
  32. case "todayReleaseToltal" :
  33. that.todayReleaseToltal = data['value'];
  34. break;
  35. default:
  36. throw 'Object传入的值应该name,value的形式传入...';
  37. }
  38. }
  39. }
  40. }

使用数据

  1. ProfitSecond profit = ProfitSecond(_data);
  2. // profit.
  3. print(profit.todayPersonSpaceEarnings);
  4. profit.setPropertyChild = {
  5. 'name' : "todayPersonSpaceEarnings",
  6. "value" : 12,
  7. };
  8. print(profit.todayPersonSpaceEarnings);
  9. print("===== ----- =====");
  10. print(profit.todayPromotionRelease);
  11. profit.setPropertyChild = {
  12. 'name' : "todayPromotionRelease",
  13. "value" : 12,
  14. };
  15. print(profit.todayPromotionRelease);
  16. print("===== ---111-- =====");
  17. print(profit.todayLock);
  18. profit.setPropertyParent = {
  19. 'name' : "todayLock",
  20. "value" : 0.000001,
  21. };
  22. print(profit.todayLock);