Home > Software design >  how to return the whole element value length that contains specific letters using String method in d
how to return the whole element value length that contains specific letters using String method in d

Time:02-03

here i have String list and String variable which cotwins the wanted letters

List myBooks ['hello world' , 'hello dart', 'hello flutter'] ;

String isBook = 'flutter' // here i target the hello flutter element 

here i have Text Widget and String Method

Text(getBook(isBook ))

String getBook(String isBook ){
 return // here i need to return the whole length of element that contains 'flutter'
so i need to get 'hello flutter'
}

How to handle this

CodePudding user response:

You can use List.firstWhere to find element and .contains() to check value;

String getBook(String isBook) {
  List myBooks = ['hello world', 'hello dart', 'hello flutter'];

  return myBooks.firstWhere((b) => b.contains(isBook));
}

void main() {
  String isBook = 'flutter';
  
  print(getBook(isBook)); // hello flutter
}
  •  Tags:  
  • Related