2

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]
FlutterFirebase
  • 2,163
  • 6
  • 28
  • 60
  • 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

3 Answers3

11

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,673
  • 3
  • 16
  • 29
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
  • 370
  • 3
  • 13
0

You can try:

List<String> emails = ["example@example.com", "Example@example.com", "example@Example.com", "example@example.cOm", "EXAMPLE@example.cOm"];
 for(String email in emails){
  print(email.toLowerCase());
}
Will28
  • 449
  • 4
  • 5