Calling method / function from another stateful class to another stateful class not working correctly using Keys

Viewed 27

So I'm trying to use a method from class BlackJacScreen in another class called BJSettings, for the implementation I tried using keys to access it.

the method I'm trying to run is

void setBal(int tempVal) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt("temp", tempVal);
setState(() {
  temp = tempVal;
});

}

so in my BJSettings page I created a globalkey variable

 GlobalKey<BlackJacScreenState> _key = GlobalKey<BlackJacScreenState>();

and a button with an onpressed

    ElevatedButton(
                onPressed: () {
                  _key.currentState?.setBal(60);
                })

how will i be able to run the _key.currentState?.setBal(60); method ? I have also even tried to run simpler method, such as a simple print method but I couldnt be able to make the function run at all, am i missing something?? I understand that this could be done using state management, but I'm just trying to learn how keys work,,, state management aside, for example if my function or method that I want to use is

void hello (){ print("Hello world"); }

at BlackJacScreen, how can i access this method using KEYS in another stateful class?

1 Answers
Related