Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to return the whole element value length that contains specific letters using String method in dart

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading