Home > database >  how to access variable and its data from different screen in flutter?
how to access variable and its data from different screen in flutter?

Time:08-22

Is there a way to access variables from different screen in flutter? What I am trying to do is i've been trying to perform a basic calculation but it won't let me since one of the variable that I am trying to calculate is defined from the other screen. Here is my code:

double amount = 0;
Center(
              child: FlatButton(
                onPressed: () {
                  setState(() {
                    balance  = amount;  // here is where I get an error since balance is not define from this screen it was define from the other screen. My plan is to add the current balance from the amount that came from the user and update that output base on this result. 
                  });
                },
                child: Text(
                  "Continue",
                  style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Color.fromARGB(255, 0, 0, 0)),
                ),

Here is my other code from the different screen:

double balance = 0;
child: Center(
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Text(
              "Current Balance: $balance",  // This is where I want to display the output base on the calculations I have done on the other screen. 
              style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),

CodePudding user response:

You can use the Provider package for flexibility. All you need is a proper setup of the provide package and you're good to go.

CodePudding user response:

Try this:

To display the balance value on the main screen you have to refresh the stateful widget. If you widget it not stateful , make it so. Add a key to it so that you can control it from the other screen and also make its state non private.

From class _MyStatefulWidgetState extends State<MyStatefulWidget>

to class MyStatefulWidgetState extends State<MyStatefulWidget> .

At at the top of your code a Global key like this: GlobalKey<MyStatefulWidgetState> widgetKey = GlobalKey();

Then when you declare your page on your main.dart file , declare it like MyStatefulWidget(key: widgetKey).

Now , you create a variable inside the state to display on the screen and give it the value of balance. double _balance = balance;

Create a method that refreshes the value.

void refreshBalance(){
 setState((){
  _balance = balance;
 });
}

and

child: Text("Current Balance: $_balance",
       style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),),

On the other page on the onPressed property add this to your code after the setState() call: widgetKey.currentState!.refreshBalance(); (thats how you call the function from within a state)

Let me know if this works

CodePudding user response:

see this answer

https://stackoverflow.com/a/73436918/19485352

you can put your variables in empty file

  • Related