import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:news_app/extension/base.dart'; import '../constant/api_const.dart'; import '../http/http_util.dart'; import '../http/model_parser.dart'; import '../model/hot_word_model.dart'; import '../model/video_new_model.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/25 13:42 /// @description: class VideoSearchProvider extends Notifier { @override VideoSearchData build() { return VideoSearchData(); } FuturefetchVideoHotWord(String type) async{ var jsonData = await HttpUtil().get( apiHotWord, queryParameters: {"q": type}, ); final newList = ModelParser.parseList( jsonData, HotWordModel.fromJson, ); state = state.copyWith(hotWords: newList); } Future fetchSearch(String keyword, int pageNum) async { final jsonData = await HttpUtil().get( apiVideoSearch, queryParameters: { "q": keyword, "pageSize": 10.toString(), "pageNum": pageNum.toString(), }, ); final response = ModelParser.parseObject( jsonData, VideoSearchModel.fromJson, ); if (pageNum == 0) { state = state.copyWith(searchModel: response); } else { //把新的列表添加到旧的列表中 state = state.copyWith(searchModel: VideoSearchModel( total: response.total, rows: [...state.searchModel?.rows ?? [], ...response.rows ?? []], )); } } } class VideoSearchModel { List? rows; int? total; VideoSearchModel({this.rows, this.total}); VideoSearchModel.fromJson(Map json) { if (json['rows'] != null) { final v = json['rows']; final arr0 = []; v.forEach((v) { arr0.add(VideoNewModel.fromJson(v)); }); rows = arr0; } total = json['total']?.toString().convertInt; } Map toJson() { final data = {}; if (rows != null) { final v = rows; final arr0 = []; for (var v in v!) { arr0.add(v!.toJson()); } data['rows'] = arr0; } data['total'] = total; return data; } } class VideoSearchData{ final VideoSearchModel? searchModel; final List hotWords; VideoSearchData({ this.searchModel, this.hotWords = const [], }); VideoSearchData copyWith({ VideoSearchModel? searchModel, List? hotWords, }){ return VideoSearchData( searchModel:searchModel ?? this.searchModel, hotWords:hotWords ?? this.hotWords, ); } }