gpt4 book ai didi

flutter - 使用 map 而不是 for 语句使类模型中的列表返回零

转载 作者:IT王子 更新时间:2023-10-29 07:18:35 25 4
gpt4 key购买 nike

当我从服务器返回的数据实现分页 ListView 时,我可以简单地测试这种分页能力并且工作正常,例如通过这段代码我可以通过简单的数据制作列表列:

list.add(
PostItem((b) =>b
..title = 'lorem ipsum'
..colorInt = _randomGenerator.nextInt(0xFFFFFFFF)),
);

return BuiltList.of(list);

现在我正在尝试从服务器获取数据并从中获取数据,例如:

List<PostItem> list = [
PostItem((b)=>b..title = 'lorem ipsum'),
PostItem((b)=>b..title = 'lorem ipsum')
];

我得到错误:

List<PostItem> list=[];

final response = await http.get(Constants.getPosts, headers: {"Accept": "application/json"});

final responseString = json.decode(response.body) as List;

List<ContentsModel> responses = responseString.map((j) =>
ContentsModel.fromJson(j)).toList();

responses.map((post)=>
list.add(PostItem((b) => b..title = post.title),));

return BuiltList.of(list);

list 的行大小为零:

responses.map((post)=>list.add(PostItem((b) => b..title = post.title),));

我的 ContentsModel 类内容是:

part 'contents_model.g.dart';

@JsonSerializable(nullable: false)
class ContentsModel{
int id;
String title;
String description;

ContentsModel(this.id, this.postId, this.title, this.description, this.type, this.featuredImages, this.slug, this.lang, this.visit, this.click, this.state, this.createdAt, this.updatedAt, this.categories);

factory ContentsModel.fromJson(Map<String,dynamic> json)=>_$ContentsModelFromJson(json);
Map<String,dynamic> toJson()=>_$ContentsModelToJson(this);
}

最佳答案

你需要初始化你的列表,然后再添加一些东西:

List<PostItem> list = [];

关于flutter - 使用 map 而不是 for 语句使类模型中的列表返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56542288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com