Create 6 Distinctive Random Numbers using Flutter and Dart to make an app. Where these 6 different numbers don’t repeat themselves when they are randomize
Although I used this
import 'dart:math';
void main() {
List<int> values = [];
while (values.length < 6) {
int randomValue = Random().nextInt(50);
if (!values.contains(randomValue)) {
values.add(randomValue);
}
}
print('Randomized values: $values');
}
But I can’t use display it on the text widget in my app
2
Answers
You can use
Set
instead ofList
.This can take quite a time based on random generation.
your code looks like pure Dart code.
And your code is just fine to generate the 6 random numbers and put it into an array.
If you have Flutter code around, you can just quickly do print the array in a
Text()
widget doing this:An entire class in Flutter using your same code could be: