Umer
Umer

Reputation: 203

How can i access value of a bool variable from a different screen in flutter?

I am trying to get the boolean value from a different screen.

in screen1 i have :

Future<void> _verifyPuchase(String id) async {
    PurchaseDetails purchase = _hasPurchased(id);
    if (purchase != null && purchase.status == PurchaseStatus.purchased) {
      print(purchase.productID);
      if (Platform.isIOS) {
        await _iap.completePurchase(purchase);
        print('Achats antérieurs........$purchase');
        isPuchased = true;
      }
      isPuchased = true;
    } else {
      isPuchased = false;
    }
  }

and on screen2 i have :

            IconButton(
                icon: Icon(Icons.call), onPressed: () => onJoin("AudioCall")),
            IconButton(
                icon: Icon(Icons.video_call),
                onPressed: () => onJoin("VideoCall")),

i am trying to get the value of isPuchased on screen2 , meaning when the any of the icon buttons are tapped on the screen2 then it first verifies the value of isPuchased and if its true then proceed with the normal execution of the function onJoin() and if its false then dont proceed

Upvotes: 0

Views: 939

Answers (1)

Moaid ALRazhy
Moaid ALRazhy

Reputation: 1744

Easiest way

first page:

Navigator.of(context).push(MaterialPageRoute(builder:(context)=>SecondPage(isPuchased)));

second page:

 class SecondPage extends StatefulWidget {
  bool isPuchased;
  SecondPage(this.isPuchased);
  @override
  State<StatefulWidget> createState() {
    return SecondPageState();
  }
}
class SecondPageState extends State<SecondPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //now you have passing variable
        title: Text("title"),
      ),
      body:Column(children: [
        IconButton(
            icon: Icon(Icons.call), onPressed: () {
      if(widget.isPuchased)  onJoin("AudioCall");
    } ),
        IconButton(
            icon: Icon(Icons.video_call),
            onPressed: () {
               if(widget.isPuchased)  onJoin("VideoCall");
            }),
      ],),
    );
  }

}

and note your problrm is a state management problem you have to search about "state managment in flutter" either using the traditional way passing the data with you in constructor until you reach the desired page, or use state management like Bloc or Provider

Upvotes: 2

Related Questions