Reputation: 1793
My question is very simple, how can i create a loop that will loop a simple list of elements.
List li=["-","\\","|","/"];
this is my dart list and i want to create this simple animation.
Upvotes: 33
Views: 89991
Reputation: 1196
there are may methods to get a loop from list for example we have this type of list
var mylist = [6, 7 ,9 ,8];
using for each method
mylist.forEach((e){
print(e);
});
using for Loop
for (int i=0 ; i<mylist.length; i++){
var e = mylist[i];
print(e);
}
Enhanced For loop
for (var e in mylist){
print(e);
}
using while loop
var i = 0;
while(i < mylist.length){
print(mylist[i]);
i++;
}
Upvotes: 4
Reputation: 1780
Another ways of looping through a List in Dart:
Using the forEach method:
li.forEach((value) {
var currentElement = value;
});
Using a While Loop and an Iterator:
// First, get an iterator to the list:
var myListIter = li.iterator;
// Iterate over the list:
while(myListIter.moveNext()){
var currentElement = myListIter.current;
}
Upvotes: 6
Reputation: 12752
Different ways to Loop through a List of elements
1 classic For
for (var i = 0; i < li.length; i++) {
// TO DO
var currentElement = li[i];
}
2 Enhanced For loop
for(final e in li){
//
var currentElement = e;
}
Notice the keyword final
. It means single-assignment, a final
variable's value cannot be changed.
3 while loop
var i = 0;
while(i < li.length){
var currentElement = li[i];
i++;
}
For the while
loop, you will use var
to reassign the variable value.
Upvotes: 75
Reputation: 1051
Try this code to loop through a list:
List li=["-","\\","|","/"];
for (var i=0; i<li.length; i++) {
print(li[i]);
}
As to the animation:
HTML
<p id="test">
test
</p>
Dart
import 'dart:html';
import 'dart:async';
main() async {
List li = ["-", "\\", "|", "/"];
for (var i = 0; i < 400000000; i++) {
querySelector('#test').text = li[i % 4];
(await new Future.delayed(const Duration(seconds: 1)));
}
}
Upvotes: 18