Home > front end >  Flutter get Images by extension form storage
Flutter get Images by extension form storage

Time:09-11

I want to know how can i compare file extension

void main() {
List<String> image = ["sdasd/dsd.png","sdafd/ddd.jpeg","sdsdd/dgd.gif","sdasd/dhd.png","sdasd/dhd.txt",];

final RegExp regExp =
    RegExp(".(gif|jpe?g|tiff?|png|webp|bmp)", caseSensitive: true);

for (var img in image) {
  print(img.contains(regExp));
 }
}

output:- true true true true false

Problem if i put ".pngk" it show me "true",

need help

CodePudding user response:

Your regex is looking for matches in the entire string, not at the end.

Add $ to end:

".(gif|jpe?g|tiff?|png|webp|bmp)\$"

$ matches position just after the last character of the string.

CodePudding user response:

void main() {
  List<String> image = [
    "d1.Png",
    "d2.jpeg",
    "d3.gif",
    "d4.png",
    "d5.txt",
  ];
  List? newImages = [];

  final RegExp regExp =
      RegExp(".(gif|jpe?g|tiff?|png|webp|bmp)\$", caseSensitive: false);

  for (var img in image) {
    if (img.contains(regExp)) {
      newImages.add(img);
      print(img);
    }
  }

  if (newImages != null) {
    print("${newImages.length}");
  }
}
  • Related