when i implementing paginating ListView
from data witch that return from server, simply i can test this paginate ability and that's work fine, for example by this code i can make list column by simple data:
list.add(
PostItem((b) =>b
..title = 'lorem ipsum'
..colorInt = _randomGenerator.nextInt(0xFFFFFFFF)),
);
return BuiltList.of(list);
now i'm trying to get data from server and make this data from that like with:
List<PostItem> list = [
PostItem((b)=>b..title = 'lorem ipsum'),
PostItem((b)=>b..title = 'lorem ipsum')
];
i get error:
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);
in this line size of list
is zero:
responses.map((post)=>list.add(PostItem((b) => b..title = post.title),));
my ContentsModel
class content is:
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);
}
You need initialize your list, before add something:
List<PostItem> list = [];
changing
responses.map((post)=>
list.add(PostItem((b) => b..title = post.title),));
to
responses.forEach((post)=>list.add(PostItem((b) =>b..title = post.title)));
resolved my problem
User contributions licensed under CC BY-SA 3.0