video_search_provider.dart 2.6 KB

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