FlutterFirebase
FlutterFirebase

Reputation: 2343

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]]

Upvotes: 2

Views: 7735

Answers (3)

Will28
Will28

Reputation: 599

You can try:

List<String> emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"];
 for(String email in emails){
  print(email.toLowerCase());
}

Upvotes: 0

Alan Cesar
Alan Cesar

Reputation: 400

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

Upvotes: 0

bluenile
bluenile

Reputation: 6029

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);

Upvotes: 11

Related Questions