skip to Main Content

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


  1. Chosen as BEST ANSWER

    I think this is the best method to find any of the extensions(png, jpg, jpeg,). This is working fine for me

    Widget AttachmentWidget(List<Attachment> attachmentmodel)
          {
            int listsize=attachmentmodel.length;
    
            int images=0;
            int videos=0;
            int audio=0;
            int others=0;
    
            Set <String> ImageExt = {'jpg','png','jpeg','tiff'};
            Set <String> VideoExt = {'mp4','mpeg4','wmp'};
            Set <String> AudioExt={'mpga'};
    
            for(int i=0;i<=listsize-1;i++)
              {
                String ext=attachmentmodel[i].ext;
    
                if(ImageExt.contains(ext))
                  {
                    images=images+1;
                  }
                else if(VideoExt.contains(ext))
                  {
                    videos=videos+1;
                  }
                else if(AudioExt.contains(ext))
                  {
                    audio=audio+1;
                  }
                else
                  {
                    others=others+1;
                  }
              }
    
    
    
            return Text('list size    $listsize n images     $images n  videos    $videos  n audios  $audio');
          }
    

  2. You can filter a list by using where, such as:

    var photos = items.where((media) => isPhoto(media.ext));
    
    var videos = items.where((media) => isVideo(media.ext));
    
    bool isPhoto(String ext) => ext == '.jpg' || ext == '.png';
    
    bool isPhoto(String ext) => ext == '.mp4';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search