| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:news_app/constant/api_const.dart';
- import 'package:news_app/http/http_util.dart';
- import 'package:news_app/model/topic_hot_model.dart';
- import 'package:news_app/model/topic_item_model.dart';
- import 'package:news_app/model/topic_rank_model.dart';
- import '../http/model_parser.dart';
- /// @author: bo.zeng
- /// @email: cnhbwds@gmail.com
- /// @date: 2025 2025/4/18 10:43
- /// @description:
- class TopicProvider extends Notifier<TopicData> {
- @override
- TopicData build() {
- return TopicData();
- }
- Future<void> fetchTopicHot() async {
- var jsonData = await HttpUtil().get(
- apiTopicHot,
- queryParameters: {"sid": "411683596922949"},
- );
- final newsList = ModelParser.parseList<TopicHotModel>(
- jsonData,
- TopicHotModel.fromJson,
- );
- //直接修改 state.topicHotList 不会触发状态更新。要确保通过 state = newState 来更新整个状态对象。
- state = state.copyWith(topicHotList: newsList);
- }
- Future<void> fetchRankHot() async {
- var jsonData = await HttpUtil().get(
- apiTopicRank,
- queryParameters: {"sid": "411683596922949", "pn": 0, "ps": 10},
- );
- final newsList = ModelParser.parseList<TopicRankModel>(
- jsonData,
- TopicRankModel.fromJson,
- );
- state = state.copyWith(topicRankList: newsList);
- }
- }
- class TopicData {
- final List<TopicHotModel> topicHotList;
- final List<TopicRankModel> topicRankList;
- TopicData({this.topicHotList = const [], this.topicRankList = const []});
- TopicData copyWith({
- List<TopicHotModel>? topicHotList,
- List<TopicRankModel>? topicRankList,
- TopicItemModel? topicItemModel,
- }) {
- return TopicData(
- topicHotList: topicHotList ?? this.topicHotList,
- topicRankList: topicRankList ?? this.topicRankList,
- );
- }
- }
|