1

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),
            ),
          ),
        ),
      ),
3
  • I think that on the main screen you also need to call setState() to display the new balance value. Commented Aug 21, 2022 at 15:22
  • Does this answer your question? how to access variable value , from a different screen in flutter? Commented Aug 21, 2022 at 15:34
  • The accepted answer is not the best answer IMO. Commented Aug 21, 2022 at 15:35

3 Answers 3

1

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

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

Comments

0

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.

1 Comment

hello, thank you for this will try to read this package and see if I can apply this with my problem! :)
0

see this answer

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

you can put your variables in empty file

Comments

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.