0

i've created a 2 lists of values. I mean, i have 2 lists with different values. Also i have a button with action to change bool value. If its true it will generate random value from cars List and false will generate random value from sportcars List. But i dont want to generate random value. I want to generate first value, then change to second, third, fourth,... and repeat this cycle

bool or = true;

child: Text(
            or ? cars[Random().nextInt(cars.length - 1)] : sportcars[Random().nextInt(sportcars.length - 1)],
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 20,
            ),
          ),

child: FloatingActionButton(
                heroTag: null,
                onPressed: () {
                  setState(() {
                    or = false;
                  });
                },

 child: FloatingActionButton(
                heroTag: null,
                onPressed: () {
                  setState(() {
                    or = true;
                  });
                },
2
  • Not clear please add some more detail Commented Apr 12, 2021 at 9:35
  • I mean, i have 2 lists with different values. Also i have a button with action to change bool value. If its true it will generate random value from cars List and false will generate random value from sportcars List. But i dont want to generate random value. I want to generate first value, then change to second, third, fourth,... and repeat this cycle Commented Apr 12, 2021 at 9:44

1 Answer 1

1

You can create two variables equal to Zero, one for each list, and whenever you press the button you can view that specific list at that specific index. Then you can increment the variable after each view so that next time you can view the next element of the list, and when the variable reaches the end of the list, simple reinitialize it to 0 again with a simple if statement. That's how I would go about it based on what I understood from you question.

void main() {

 var list1 = [1,3,5,7];
 var var1 =0;

 var list2 = [2,4,6,8];
 var var2 =0;  

 bool value = false;

 if(value == false){
    print(list1[var1]);
    var1++;
 }else if(value == true){
    print(list2[var2]);
    var2++;
 }
}

this is what I have in mind, and each time you change the value between true and false you run this if condition, and it will also increment so you can view next element.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you please show me that in code please. Because i’m not sure if i understand what you mean.
I have coded something in dart playground hope that helps with your case look at my edited Answer
How can i implement if statement to Text() widget?
I believe you can do something like, Text( or ? cars[var1] : cars[var2]) and with each button press modify the values of var1 and var2. that way with true and false you choose which list to choose from and with var1 and var2 you get each element.
i added in var1++ and var2++ works fine, but how can i reset value of var when its on the last thing
|

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.