The reason is DartPad does only first show output when there are no more work to do (e.g. waiting on something async or the program are done).
In you case, your program contains an infinite loop so the program are just going to run endlessly keeping adding elements into the list until you run out of memory. This can be seen if you add some additional logging to your program and run it in DartVM which does print as the program are running.
You problem can be found here:
for(int j=0; j<i; j++){
if(myNum[i]==myNum[j]){
i--;
break;
}
}
There are better ways to detect multiple values (and some could argue to use a Set instead) but the main problem is you are never removing the detected element from the list. Instead you are just counting i down. When inserting elements into the list you are using:
myNum.add(num);
Which just continues to add elements to the existing list of values. So you program are running fine until if(myNum[i]==myNum[j]) is true which will happen at random (and a lot of time never happen which is why you program often seem to be fine).
You should add a myNum.removeLast() to your logic as shown here:
for (int j = 0; j < i; j++) {
if (myNum[i] == myNum[j]) {
i--;
myNum.removeLast();
break;
}
}
Then it works as expected.
Update with example of simpler solution
A Set cannot contain the same value multiple times. So if we try insert an element which are already part of the Set the length are not changed. So we can basically just do the following:
import 'dart:math';
void main() {
final myNum = <int>{};
final random = Random();
while (myNum.length != 6) {
myNum.add(random.nextInt(45) + 1);
}
print(myNum.toList()); // [2, 23, 40, 35, 39, 22]
}
Even shorter solution
If you want to minimize the code and also have stable execution time you can also do the following:
void main() {
print((List.generate(45, (index) => ++index)..shuffle()).sublist(0, 6)); // [6, 41, 12, 2, 11, 42]
}