Home > Software design >  How to use initState from another file on flutter?
How to use initState from another file on flutter?

Time:04-04

I'm trying to import the information of initState that stores the state of my Switch.

The problem is that initState is on my homepage file and I want to import it from my "error page".

Example:

  • If my Switch is on, my theme will change.

  • I want to import that Switch info (boolean) on every page of my app.

  • I want to be able to change that Switch value on other pages too.

Here is my homepage:

class _MyHomePageState extends State\<MyHomePage\> {
bool isSwitchedFT = false;
void updateSwitchValue(bool newValue)
  {
    setState(() {
      isSwitchedFT = newValue;
    });
  }

@override
void initState(){
super.initState();
getSwitchValues();
}
getSwitchValues() async {
isSwitchedFT = (await getSwitchState())!;
setState(() {});
}

Future <bool> saveSwitchState(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("switchState", value);
return prefs.setBool("switchState", value);
}

Future <bool?> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? isSwitchedFT = prefs.getBool("switchState");

    return isSwitchedFT;

}

@override
Widget build(BuildContext context) {
return Scaffold(){...} 

And here is my other page:

class _ErrorPageState extends State<ErrorPage> {

/// I want to get this boolean info from MyHomePage initState
   bool isSwitchedFT = false;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(...);

Here is my Switch:

Switch(
                  value: isSwitchedFT,
                  onChanged: (bool value) {
                    setState(() {
                      isSwitchedFT = value;
                      saveSwitchState(value);
                      //switch works
                    });
                  })

this is my complete code from the second page:

class SubmitErrorPage extends StatelessWidget {
  const SubmitErrorPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ErrorPage(),
    );
  }
}

class ErrorPage extends StatefulWidget {


  const ErrorPage({Key? key}) : super(key: key);

  @override
  State<ErrorPage> createState() => ErrorPageState();
}

class ErrorPageState extends State<ErrorPage> {


  bool isSwitchedFT = true;

  GlobalKey<MyHomePageState> myKey = GlobalKey();
  //get the value of the switch
  bool getSwitchValueFromHomePage()
  {
    return myKey.currentState!.isSwitchedFT;
  }
  //update the switch
  void updateSwitchValue(bool newValue)
  {
    myKey.currentState!.updateSwitchValue(newValue); // you need to implement this function in your MyHomePageState
  }



  @override
  Widget build(BuildContext context) {
    return SafeArea(
child: Scaffold(
          backgroundColor: isSwitchedFT ? Colors.black : Colors.white,){...}

CodePudding user response:

you access the variable from your state using GlobalKey like:

 static GlobalKey<MyHomePageState> myKey = GlobalKey();

 myKey.currentState!.isSwitchedFT; //here

where you have to remove the _ part from your state to be accessible.

edit: the logic in your code would look something like:

class _ErrorPageState extends State<ErrorPage> {

  /// I want to get this boolean info from MyHomePage initState
  bool isSwitchedFT = false;
  GlobalKey<MyHomePageState> myKey = GlobalKey();
  //get the value of the switch 
  bool getSwitchValueFromHomePage()
  {
    return myKey.currentState!.isSwitchedFT;
  }
  //update the switch
  void updateSwitchValue(bool newValue)
  {
    myKey.currentState!.updateSwitchValue(newValue); // you need to implement this function in your MyHomePageState
  }
  
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(...);
  }
}

the updateSwitchValue would look like:

void updateSwitchValue(bool newValue)
{
  setState(() {
       this.isSwitchedFT = newValue;
    });
}

and you need to add it to your MyHomePageState.

  • Related