msg_center_provider.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/constant/api_const.dart';
  3. import 'package:news_app/extension/base.dart';
  4. import 'package:news_app/model/msg_center_model.dart';
  5. import '../http/http_util.dart';
  6. import '../http/model_parser.dart';
  7. class MsgCenterLikeProvider extends FamilyNotifier<UserMsgLike, String> {
  8. String _type = "reply"; //reply like
  9. @override
  10. UserMsgLike build(String type) {
  11. _type = type;
  12. return UserMsgLike();
  13. }
  14. Future<void> fetchMsgCenterLike({required int pageNum}) async {
  15. var jsonData = await HttpUtil().get(
  16. apiMsgCenter,
  17. queryParameters: {"type": _type, "pageSize": "10", "pageNum": pageNum},
  18. );
  19. final response = ModelParser.parseObject<UserMsgLike>(
  20. jsonData,
  21. UserMsgLike.fromJson,
  22. );
  23. state.total = response.total;
  24. if (pageNum == 0) {
  25. state = response;
  26. } else {
  27. //把新的列表添加到旧的列表中
  28. state = UserMsgLike(
  29. total: response.total,
  30. rows: [...state.rows ?? [], ...response.rows ?? []],
  31. );
  32. }
  33. }
  34. }
  35. class UserMsgLike {
  36. List<MsgCenterModelRows?>? rows;
  37. int? total;
  38. UserMsgLike({this.rows, this.total});
  39. UserMsgLike.fromJson(Map<String, dynamic> json) {
  40. if (json['rows'] != null) {
  41. final v = json['rows'];
  42. final arr0 = <MsgCenterModelRows>[];
  43. v.forEach((v) {
  44. arr0.add(MsgCenterModelRows.fromJson(v));
  45. });
  46. rows = arr0;
  47. }
  48. total = json['total']?.toString().convertInt;
  49. }
  50. Map<String, dynamic> toJson() {
  51. final data = <String, dynamic>{};
  52. if (rows != null) {
  53. final v = rows;
  54. final arr0 = [];
  55. for (var v in v!) {
  56. arr0.add(v!.toJson());
  57. }
  58. data['rows'] = arr0;
  59. }
  60. data['total'] = total;
  61. return data;
  62. }
  63. }