'Random List Flutter
I have a list that generates random numbers. I'm printing every element in this list to the screen. And I take three random elements from the list and determine the target number. but every time I call numberList it sends me a separate list. Therefore, the number list I print on the screen and the numberList in the target() function are different and the target number is wrong. And I need to update the list when needed . How can i solve this.
My code
class GamePage extends StatefulWidget {
const GamePage({Key? key}) : super(key: key);
@override
GamePageState createState() => GamePageState();
}
class GamePageState extends State<GamePage> {
int _counter = 20;
Timer? _timer;
int _score = 0;
Color _color = Colors.green;
static List numberList = List.generate(6, (index) => Random().nextInt(10));
static List changeList() => List.generate(6, (index) => Random().nextInt(10));
static List colorList = Util.colorList;
static int target() {
int target = 0;
List list = Util.targetRandom();
int num = 2;
while (num >= 0) {
int index = list[num];
target = numberList[index] + target;
num--;
}
print(numberList);
return target;
}
void _updateList() {
_updateButtonColor();
numberList = changeList();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
