1

I need to display a random number in flutter using a function, but when i call it i get error.

 void generateRandomNumber() {
                        setState(() {
                          point=pointValue[new Random().nextInt(pointValue.length)];
                        });
                        }
7
  • what is the error? Commented Jun 28, 2020 at 11:52
  • the error is when i call it does not give any result on my app screen Commented Jun 28, 2020 at 11:55
  • I am assuming pointValue is your list, do you have any item there? Commented Jun 28, 2020 at 11:55
  • yes here is the declaration of list List<int> pointValue = [5,6,7,8,10,15]; Commented Jun 28, 2020 at 11:58
  • I want to turn this void method to function that return a number on my screen, and i'm very thankful for your time Commented Jun 28, 2020 at 11:59

1 Answer 1

2

Return int value from your method which generated by Random class

 List<int> pointValue = [5,6,7,8,10,15];
  
  int generateRandomNumber() {
    return pointValue[new Random().nextInt(pointValue.length)];
  }
Sign up to request clarification or add additional context in comments.

6 Comments

Thaaanks a lot, i change the code like you but when i call generateRandomNumber() i get the error 'The element type 'int' can't be assigned to the list type 'Widget'.'
generateRandomNumber(); Center( child:Text( 'elements', style: TextStyle( color: Colors.yellowAccent, fontWeight: FontWeight.bold, fontSize: 16, fontFamily: 'muso', decoration: TextDecoration.underline), ), )
generateRandomNumber().toString() instead of 'elements'.
Thaaaaaank you you save my day
Hi, can you help me please i have a problem that i don't know how can i get the result that i get on this screen to another screen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.