'How to convert all string in Dart list to lowercase?

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:

[[email protected], [email protected], [email protected], [email protected], [email protected]]


Solution 1:[1]

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

  List<String> emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"];
  emails = emails.map((email)=>email.toLowerCase()).toList();
  print(emails);

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bluenile
Solution 2 Alan Cesar