| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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<VideoSearchData> {
- @override
- VideoSearchData build() {
- return VideoSearchData();
- }
- Future<void>fetchVideoHotWord(String type) async{
- var jsonData = await HttpUtil().get(
- apiHotWord,
- queryParameters: {"q": type},
- );
- final newList = ModelParser.parseList<HotWordModel>(
- jsonData,
- HotWordModel.fromJson,
- );
- state = state.copyWith(hotWords: newList);
- }
- Future<void> 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<VideoSearchModel>(
- 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<VideoNewModel?>? rows;
- int? total;
- VideoSearchModel({this.rows, this.total});
- VideoSearchModel.fromJson(Map<String, dynamic> json) {
- if (json['rows'] != null) {
- final v = json['rows'];
- final arr0 = <VideoNewModel>[];
- v.forEach((v) {
- arr0.add(VideoNewModel.fromJson(v));
- });
- rows = arr0;
- }
- total = json['total']?.toString().convertInt;
- }
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{};
- 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<HotWordModel> hotWords;
- VideoSearchData({
- this.searchModel,
- this.hotWords = const [],
- });
- VideoSearchData copyWith({
- VideoSearchModel? searchModel,
- List<HotWordModel>? hotWords,
- }){
- return VideoSearchData(
- searchModel:searchModel ?? this.searchModel,
- hotWords:hotWords ?? this.hotWords,
- );
- }
- }
|