/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 18:02:13
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-04-27 15:50:39
 * @Descripttion: 
 */
import 'package:flutter/material.dart';

class PagingModel<@required T> {
  List<T>? list;
  num? pageNum;
  num? pageSize;
  num? pages;
  num? total;

  late Function _itemToJson;

  ///[json] 传入接口返回的data数据
  ///
  ///[from] 传入List Item的构造函数fromJson
  ///
  ///[to] 传入List Item的构造函数fromJson
  ///
  PagingModel({
    required Map<String, dynamic> json,
    required T Function(dynamic dataJson) from,
    required Function(T data) to,
  }) {
    _itemToJson = to;
    _fromJson(json);
    if (json['list'] != null) {
      list = json['list'].map<T>((item) => from(item)).toList();
    }
  }
  _fromJson(json) {
    Map<String, dynamic> map = Map.from(json ?? {});
    pageNum = map['pageNum'];
    pageSize = map['pageSize'];
    pages = map['pages'];
    total = map['total'];
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = Map<String, dynamic>();

    json['list'] = List<dynamic>.from((list ?? []).map((x) => _itemToJson(x)));
    json['pageNum'] = this.pageNum;
    json['pageSize'] = this.pageSize;
    json['pages'] = this.pages;
    json['total'] = this.total;

    return json;
  }
}
