Get the value of a variable in another widget

2,596

Make variable1 a global variable (Don't declare it in any class or method). You can access it anywhere in the app.

For example...


var variable1 //variable1 is accessible everywhere because it is top-level

class Page1 extends StatelessWidget{

   @override
   Widget build(){}
}

class Page2 extends StatelessWidget{

   @override
   Widget build(){}
}

Share:
2,596
simi
Author by

simi

Updated on December 20, 2022

Comments

  • simi
    simi over 1 year

    I have a question. I have in my app an AppBar with a dropdown menu. In this dropdown menu the user can click on a few checkboxes. If a checkbox is pressed a variable1 gets a value. The problem is, that I need this value in another widget, which creats the body. I´m only able to push the value to another widget with that code:

    Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => BodyConstruction(value: exerciseToTake),
        ));
    

    But I have this code in the function, which is called if a checkbox bool changes, so the user comes always when he clicks on a checkbox directly to the other screen. My question now is, is there a posibility to get the value of a veriable in another widget without changing the screen?