我想在每个 api 调用中再获得 5 条记录并将它们添加到现有列表中。但是,下面的结构给出了错误。错误屏幕截图和完整的代码结构如下所示。
探索页面初始化
@override
void initState() {
exploreController.fetchArticles(offsetCount: page);
super.initState();
_sc.addListener(() {
if (_sc.position.pixels == _sc.position.maxScrollExtent) {
page = page + 5;
exploreController.fetchArticles(offsetCount: page);
}
});
}
探索模型
List<ExploreModel> exploreModelFromJson(String str) => List<ExploreModel>.from(
json.decode(str).map((x) => ExploreModel.fromJson(x)));
String exploreModelToJson(List<ExploreModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
我的 api 调用
static Future<List<ExploreModel>> fetchArticleList(int offsetCount) async {
var url = Uri.http(Config().baseUrl, Config().baseUrlPathGetArticles,
{'offsetCount': '$offsetCount'});
var response = await http.get(url);
if (response.statusCode == 200) {
return exploreModelFromJson(utf8.decode(response.bodyBytes));
} else {
return null;
}
}
和我的控制器
class ExploreController extends GetxController {
var isLoading = true.obs;
var articleList = <ExploreModel>[].obs;
var offsetCount = 0;
@override
void onInit() {
fetchArticles();
super.onInit();
}
void fetchArticles({int offsetCount = 0}) async {
try {
isLoading(true);
var articles = await ApiService.fetchArticleList(offsetCount);
if (articles != null) {
for (int i = 0; i < articles.length; i++) {
articleList.addAll(articles[i]);
}
}
} finally {
isLoading(false);
}
}
}