How to iterate over a Dart List using

Posted by

  • forEach() expression.
  • iterator property to get Iterator that allows iterating.
  • every() method
  • simple for-each loop
  • for loop with item index

forEach() expression:

void main()
 {
var myList = [0, 'one', 'two', 'three', 'four', 'five'];
myList.forEach((item) => print(item));
 }

Output:

0
one
two
three
four
five

Iterator property to get Iterator that allows iterating:

void main()
 {
     var myList = [0, 'one', 'two', 'three', 'four', 'five'];
     var listIterator = myList.iterator;
     while (listIterator.moveNext()) {
     print(listIterator.current);
     }
 }

Output

 0
one
two
three
four
five

every() method:

 void main()
 {
     var myList = [0, 'one', 'two', 'three', 'four', 'five'];
     myList.every((item) {
     print(item);
     return true;
     });
 }

Output

0
one
two
three
four
five

simple for-each loop:

void main()
 {
     var myList = [0, 'one', 'two', 'three', 'four', 'five'];
     for (var item in myList) {
     print(item);
     }
 }

Output

0
one
two
three
four
five

for loop with item index:

void main()
 {
     var myList = [0, 'one', 'two', 'three', 'four', 'five'];
    for (var i = 0; i < myList.length; i++) {
    print(myList[i]);
     }
 }

Output

0
one
two
three
four
five

How to Replace a Substring of a String in Dart?

main() {
    String data = "welcome";
      
    //replace substring of the given string
    String result = data.replaceAll("hii", "Abhishek!");
      
    print(result);
}

OutPut

hii Abhishek!
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x