Flutter-参数类型"Iterable>"无法分配为“列表"类型 [英] Flutter - The argument type 'Iterable>' can't be assigned to type 'List'

查看:107
本文介绍了Flutter-参数类型"Iterable>"无法分配为“列表"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个api调用中再获得5条记录,并将它们添加到现有列表中.但是,下面的结构给出了错误.错误屏幕快照和完整的代码结构在下面提供.

I want to get 5 more records in each api call and add them to the existing list. However, the structure below gives an error. The error screenshot and the complete code structure is available below.

浏览页面初始化

 @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);
      }
    });
  }

ExploreModel

ExploreModel

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);
    }
  }
}

这就是我要制作的

推荐答案

更改此内容:

List<ExploreModel> exploreModelFromJson(String str) => List<ExploreModel>.from(
    json.decode(str).map((x) => ExploreModel.fromJson(x)));

对此:

List<ExploreModel> exploreModelFromJson(String str) => List<ExploreModel>.from(
    json.decode(str).map((x) => ExploreModel.fromJson(x)).toList()); //add the toList().

这篇关于Flutter-参数类型"Iterable&gt;"无法分配为“列表"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆