I’m retrieving the API data as below format I made a Model class for that (see below code), in the JSON response ext
means an extension of the attachment (image or video), So I want to show this response as a post(like a Facebook post) I found one method to do that, first counts the image and video (How many images and how many videos) then according to the counts make widgets like Facebook, I’m using listview.builder()
. but in the listview.builder()
in one time I can’t count images or videos per onetime. So I want to get the count number how many images? how many videos? using my ext
in the JSON response
see my method
List<Attachment> attachmentmodel=widget.newsFeeds.data.data[index].attachment;
@override
Widget build(BuildContext context) {
int listsize = widget.newsFeeds.data.data.length;
return Scaffold(
body: Padding(
padding: EdgeInsets.only(right: 4,left: 4,top: 4,bottom: 4),
child: ListView.builder(
itemCount: listsize,
itemBuilder: (context, index) {
var allAttachments=widget.newsFeeds.data.data[index].attachment;
List<Attachment> attachmentmodel=widget.newsFeeds.data.data[index].attachment;
int textlength = textStatus.length;
return AttachmentWidget(attachmentmodel);
);
}
}
Implemented method
Widget AttachmentWidget(List<Attachment> attachmentmodel)
{
int listsize=attachmentmodel.length;
return Container(
height: 200,
child: ListView.builder(itemCount: listsize,
itemBuilder: (context,index)
{
return listsize>=5 ? Text('morethan 5 attachments') : Text('less than 5 attachments');
}),
);
}
My JSON
"attachment": [
{
"id": 42,
"news_feed_id": 283,
"path": "imagepath",
"ext": "png",
"created_at": "2020-03-19 10:22:26",
"updated_at": null,
"deleted_at": null
},
{
"id": 43,
"news_feed_id": 283,
"path": "imagepathf",
"ext": "png",
"created_at": "2020-03-19 10:22:27",
"updated_at": null,
"deleted_at": null
},
{
"id": 44,
"news_feed_id": 283,
"path": "imagepath40a4",
"ext": "png",
"created_at": "2020-03-19 10:22:27",
"updated_at": null,
"deleted_at": null
},
{
"id": 45,
"news_feed_id": 283,
"path": "image path",
"ext": "png",
"created_at": "2020-03-19 10:22:27",
"updated_at": null,
"deleted_at": null
}
],
This is my model class
class Attachment {
int id;
int newsFeedId;
String path;
dynamic ext;
DateTime createdAt;
dynamic updatedAt;
dynamic deletedAt;
Attachment({
this.id,
this.newsFeedId,
this.path,
this.ext,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
factory Attachment.fromJson(Map<String, dynamic> json) => Attachment(
id: json["id"],
newsFeedId: json["news_feed_id"],
path: json["path"],
ext: json["ext"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: json["updated_at"],
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"news_feed_id": newsFeedId,
"path": path,
"ext": ext,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt,
"deleted_at": deletedAt,
};
}
2
Answers
You can filter a list by using
where
, such as: