user_score_provider.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/extension/base.dart';
  3. import '../constant/api_const.dart';
  4. import '../http/http_util.dart';
  5. import '../http/model_parser.dart';
  6. import '../model/user_score_model.dart';
  7. /// @author: bo.zeng
  8. /// @email: cnhbwds@gmail.com
  9. /// @date: 2025 2025/4/22 16:03
  10. /// @description:
  11. class UserScoreProvider extends Notifier<UserScoreData> {
  12. @override
  13. UserScoreData build() {
  14. return UserScoreData();
  15. }
  16. Future<void> fetchUserScoreList(int pageNum) async {
  17. final jsonData = await HttpUtil().get(
  18. apiMemberScoreLevel,
  19. queryParameters: {"pageSize": 10, "pageNum": pageNum.toString()},
  20. );
  21. final response = ModelParser.parseObject<UserScoreData>(
  22. jsonData,
  23. UserScoreData.fromJson,
  24. );
  25. state.total = response.total;
  26. if (pageNum == 0) {
  27. state = response;
  28. } else {
  29. //把新的列表添加到旧的列表中
  30. state = UserScoreData(
  31. total: response.total,
  32. rows: [...state.rows ?? [], ...response.rows ?? []],
  33. );
  34. }
  35. }
  36. /* Future<void> fetchUserScore() async {
  37. final jsonData = await HttpUtil().get(apiMemberLevel);
  38. }*/
  39. }
  40. class UserScoreData {
  41. List<UserScoreModelRows?>? rows;
  42. int? total;
  43. UserScoreData({this.rows, this.total});
  44. UserScoreData.fromJson(Map<String, dynamic> json) {
  45. if (json['rows'] != null) {
  46. final v = json['rows'];
  47. final arr0 = <UserScoreModelRows>[];
  48. v.forEach((v) {
  49. arr0.add(UserScoreModelRows.fromJson(v));
  50. });
  51. rows = arr0;
  52. }
  53. total = json['total']?.toString().convertInt;
  54. }
  55. Map<String, dynamic> toJson() {
  56. final data = <String, dynamic>{};
  57. if (rows != null) {
  58. final v = rows;
  59. final arr0 = [];
  60. for (var v in v!) {
  61. arr0.add(v!.toJson());
  62. }
  63. data['rows'] = arr0;
  64. }
  65. data['total'] = total;
  66. return data;
  67. }
  68. }