bool Globalveriable = false;

    class Class1 extends StatefulWidget {
      @override
      _Class1State createState() => _Class1State();
    }
    
    class _Class1State extends State<Class1> {
      @override
      Widget build(BuildContext context) {
        return DayNightSwitch(
          value: Globalveriable,
          onChanged: (changed) {
            setState(() {
              Globalveriable = changed;
            });
          },
        );
      }
    }
    
    class Class2 extends StatefulWidget {
      @override
      _Class2State createState() => _Class2State();
    }
    
    class _Class2State extends State<Class2> {
      @override
      Widget build(BuildContext context) {
        return Text(Globalveriable.toString());
      }
    }

//when clicking on the switch, the value of Globalveriable will be true for Class1 but I want it should be the same for Class2 at the Same time

1

There are 1 best solutions below

0
On

add the static keyword will resolve your issue as

static bool Globalveriable = false;