Home > Net >  get access to a variable in class B and use it in class A
get access to a variable in class B and use it in class A

Time:02-21

I want to get access to variable ' a ' of class ' B' and use it in the condition of A class. what would i give to condition of class A ?

class A extends StatefulWidget 
{
    const A({ Key? key }) : super(key: key);

    @override
    _AState createState() => _AState();
}
class _AState extends State<A> 
{
  String picture = "a";
  String title = "b";    
  @override
  Widget build(BuildContext context) 
{
  if ( ??????? == 1) {
      setState(() {
        picture = "t";
        title = "y";
   return Scaffold()....
}
class B extends StatelessWidget 
{`enter code here`
  int? a;
  @override
  Widget build(BuildContext context) 
{
    return GestureDetector(
    child: Container(...)),
    onTap: () 
    {
        a = 1;
    },  
  }
}

CodePudding user response:

Please Try This.

You first your variable value put in static variable.

class StoreClassBValue{

static int a;
}

And onTap method for class B:

onTap: () 
    {
       StoreClassBValue.a =a = 1;
    },  

And if statement for classA:

if(StoreClassBValue.a==1)
{

//more your code
}

More methode are available like provide, bloc, getx but this is very easy.

  • Related