1

I want check if Dart list contain string with list.contains so must convert string in array to lowercase first.

How to convert all string in list to lowercase?

For example:

[example@example.com, Example@example.com, example@Example.com, example@example.cOm, EXAMPLE@example.cOm]
Paresh Mangukiya
  • 37,512
  • 17
  • 201
  • 182
FlutterFirebase
  • 1,733
  • 4
  • 22
  • 50
  • 1
    You can use .any() instead. if (["aBc", "bca"].any((el) => el.toLowerCase() == "abc")) print("contains"); – Oleg Plotnikov Dec 05 '20 at 15:55
  • Beware... the local part of the address (to the left of the @) might be _case-sensitive_ (https://stackoverflow.com/questions/9807909/are-email-addresses-case-sensitive). Although you probably won't run into issues, best to leave that alone if you can. – Randal Schwartz Dec 05 '20 at 20:07

2 Answers2

10

You can map through the entire list and convert all the items to lowercase. Please see the code below.

  List<String> emails = ["example@example.com", "Example@example.com", "example@Example.com", "example@example.cOm", "EXAMPLE@example.cOm"];
  emails = emails.map((email)=>email.toLowerCase()).toList();
  print(emails);
bluenile
  • 5,053
  • 3
  • 14
  • 25
0

Right now with Dart you can use extension methods to do this kind of conversions

extension LowerCaseList on List<String> {
  void toLowerCase() {
    for (int i = 0; i < length; i++) {
      this[i] = this[i].toLowerCase();
    }
  }
}

When you import it, you can use it like

List<String> someUpperCaseList = ["QWERTY", "UIOP"];

someUpperCaseList.toLowerCase();

print(someUpperCaseList[0]); // -> qwerty
Alan Cesar
  • 182
  • 2
  • 10