使用方法中的对象工厂模式:
使用 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.
数据传入层
Wallet _wallet = Wallet.fromJson(res.data['data']['wallet']);
数据类型定义层 — 使用类的工厂模式
import 'package:json_annotation/json_annotation.dart';part 'wallet.g.dart';@JsonSerializable()class Wallet {Wallet();String wallet_id;String user_id;num usdt_balance;num fil_balance;num lamb_balance;num user_space_num;factory Wallet.fromJson(Map<String,dynamic> json) => _$WalletFromJson(json);Map<String, dynamic> toJson() => _$WalletToJson(this);}
数据返回层
// GENERATED CODE - DO NOT MODIFY BY HANDpart of 'wallet.dart';// **************************************************************************// JsonSerializableGenerator// **************************************************************************Wallet _$WalletFromJson(Map<String, dynamic> json) {return Wallet()..wallet_id = json['wallet_id'] as String..user_id = json['user_id'] as String..usdt_balance = json['usdt_balance'] as num..fil_balance = json['fil_balance'] as num..lamb_balance = json['lamb_balance'] as num..user_space_num = json['user_space_num'] as num;}Map<String, dynamic> _$WalletToJson(Wallet instance) => <String, dynamic>{'wallet_id': instance.wallet_id,'user_id': instance.user_id,'usdt_balance': instance.usdt_balance == 0 ? "0.000000 FIL" : instance.usdt_balance ,'fil_balance': instance.fil_balance,'lamb_balance': instance.lamb_balance,'user_space_num': instance.user_space_num};


子机继承 - 数据处理
子数据
import 'package:nebula_app/models/profitParent.dart';class ProfitSecond extends ProfitParent{num todayPersonSpaceEarnings ; // 个人存储空间收益num todayPromotionRelease ; // 今日推广释放/// 继承 -- 父级// num todayProfitToltal; // 今日总收益// num todayReleaseToltal; // 今日总释放收益// num todayPledge; // 今日前置质押// num todayLock; // 今日锁仓// num todayCompanyPledge; // 今日公司质押ProfitSecond(Map<String, dynamic> json) : super.init(json){this.todayPersonSpaceEarnings = json['release_person'];this.todayPromotionRelease = json['release_manager'];}// 获取个人空间收益get getSpace{final _data = todayPersonSpaceEarnings == null ? '0.000000 FIL' : todayPersonSpaceEarnings;print("哦买噶我是个人空间收益 --- $_data");return _data;}// 获取推广收益get getPromotion{final _data = todayPromotionRelease == null ? '0.000000 FIL' : todayPromotionRelease;print("哦买噶我是推广收益 --- $_data");return _data;}// 设置 当前属性的值.set setPropertyChild(data){final that = this;// 数据为 Objectif(data is Map){final name = data['name'];// final value = data['value'];// print(value);switch (name) {case "todayPersonSpaceEarnings":that.todayPersonSpaceEarnings = data['value'];break;case "todayPromotionRelease" :that.todayPromotionRelease = data['value'];break;default:throw 'Object传入的值应该name,value的形式传入...';}}}/// 设置个人空间数据// set getSpace(val){// print('别动修改我好吗? $val');// this.todayPersonSpaceEarnings = val;// }// /// 设置推广收益// set getPromotion(val){// this.todayPromotionRelease = val;// }}
父数据
class ProfitParent{num todayProfitToltal; // 今日总收益num todayReleaseToltal; // 今日总释放收益num todayPledge; // 今日前置质押num todayLock; // 今日锁仓num todayCompanyPledge; // 今日公司质押ProfitParent.init(data){this.todayProfitToltal = data['profit_total_today'];this.todayLock = data['release_total_today'];this.todayCompanyPledge= data['locked_total_today'];this.todayPledge = data['today_pledge'];this.todayReleaseToltal = data['today_company_pledge'];}set setPropertyParent(data){final that = this;// 数据为 Objectif(data is Map){final name = data['name'];switch (name) {case "todayProfitToltal":that.todayProfitToltal = data['value'];break;case "todayLock" :that.todayLock = data['value'];break;case "todayCompanyPledge" :that.todayCompanyPledge = data['value'];break;case "todayPledge" :that.todayPledge = data['value'];break;case "todayReleaseToltal" :that.todayReleaseToltal = data['value'];break;default:throw 'Object传入的值应该name,value的形式传入...';}}}}
使用数据
ProfitSecond profit = ProfitSecond(_data);// profit.print(profit.todayPersonSpaceEarnings);profit.setPropertyChild = {'name' : "todayPersonSpaceEarnings","value" : 12,};print(profit.todayPersonSpaceEarnings);print("===== ----- =====");print(profit.todayPromotionRelease);profit.setPropertyChild = {'name' : "todayPromotionRelease","value" : 12,};print(profit.todayPromotionRelease);print("===== ---111-- =====");print(profit.todayLock);profit.setPropertyParent = {'name' : "todayLock","value" : 0.000001,};print(profit.todayLock);
