Home > Back-end >  Flutter: New variable from function
Flutter: New variable from function

Time:08-10

I have a variable which gets a value from a function. The function generates a random string from a list. However when I call the variable it has the value from the first time it was called. How do I get the variable to call the function every time so it has a new value each time it is called?

My code is:

player1gen(List players) {
  //players[Random().nextInt(players.length)];
  return players[Random().nextInt(players.length)].toString();
}

String player1 = player1gen(players);

CodePudding user response:

You can do

String get player1 => player1gen(players);
  • Related