activity_search_provider.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/activity_model.dart';
  7. import '../model/hot_word_model.dart';
  8. class ActivitySearchProvider extends Notifier<ActivitySearchData> {
  9. @override
  10. ActivitySearchData build() {
  11. return ActivitySearchData();
  12. }
  13. Future<void>fetchActivityHotWord(String type) async{
  14. var jsonData = await HttpUtil().get(
  15. apiHotWord,
  16. queryParameters: {"q": type},
  17. );
  18. final newList = ModelParser.parseList<HotWordModel>(
  19. jsonData,
  20. HotWordModel.fromJson,
  21. );
  22. state = state.copyWith(hotWords: newList);
  23. }
  24. Future<void> fetchSearch(String keyword, int pageNum) async {
  25. final jsonData = await HttpUtil().get(
  26. apiActivitySearch,
  27. queryParameters: {
  28. "q": keyword,
  29. "pageSize": 10.toString(),
  30. "pageNum": pageNum.toString(),
  31. },
  32. );
  33. final response = ModelParser.parseObject<ActivitySearchModel>(
  34. jsonData,
  35. ActivitySearchModel.fromJson,
  36. );
  37. if (pageNum == 0) {
  38. state = state.copyWith(searchModel: response);
  39. } else {
  40. //把新的列表添加到旧的列表中
  41. state = state.copyWith(searchModel: ActivitySearchModel(
  42. total: response.total,
  43. rows: [...state.searchModel?.rows ?? [], ...response.rows ?? []],
  44. ));
  45. }
  46. }
  47. }
  48. class ActivitySearchModel {
  49. List<ActivityModelRecord?>? rows;
  50. int? total;
  51. ActivitySearchModel({this.rows, this.total});
  52. ActivitySearchModel.fromJson(Map<String, dynamic> json) {
  53. if (json['rows'] != null) {
  54. final v = json['rows'];
  55. final arr0 = <ActivityModelRecord>[];
  56. v.forEach((v) {
  57. arr0.add(ActivityModelRecord.fromJson(v));
  58. });
  59. rows = arr0;
  60. }
  61. total = json['total']?.toString().convertInt;
  62. }
  63. Map<String, dynamic> toJson() {
  64. final data = <String, dynamic>{};
  65. if (rows != null) {
  66. final v = rows;
  67. final arr0 = [];
  68. for (var v in v!) {
  69. arr0.add(v!.toJson());
  70. }
  71. data['rows'] = arr0;
  72. }
  73. data['total'] = total;
  74. return data;
  75. }
  76. }
  77. class ActivitySearchData{
  78. final ActivitySearchModel? searchModel;
  79. final List<HotWordModel> hotWords;
  80. ActivitySearchData({
  81. this.searchModel,
  82. this.hotWords = const [],
  83. });
  84. ActivitySearchData copyWith({
  85. ActivitySearchModel? searchModel,
  86. List<HotWordModel>? hotWords,
  87. }){
  88. return ActivitySearchData(
  89. searchModel: searchModel ?? this.searchModel,
  90. hotWords:hotWords ?? this.hotWords,
  91. );
  92. }
  93. }