skip to Main Content

I am writing to you for support on Getx.
I currently have a GetView class where there is a button that needs to call a method defined inside the controller.
This method makes a call to a server and then after the call, it must show a BottomSheet.
Unfortunately, and I think this is my limit on GETX, I noticed that the BottomSheet is shown immediately without waiting for the response from the backend.
I tried with the wait but unfortunately without success.
Is there any tutorial I can view?
Thank you all.

Code method controller

Future<bool> increaseWallet() async {
    
    try{
      await MangoPay.instance.makePayment(
          mangoPayUserId: user!.mangoPayId!, 
          mangoPayPaymentWalletId: user!.walletId!, 
          cardData: _cardData, amount: double.parse(amount), 
            paymentCallback: (result, message, transactionId, status) async {
            if(status=="SUCCEEDED"){
              hideLoader();
              _bContinue = true;
            }
      });
    }
    catch(e){
      hideLoader();
      return bContinue;
    }
    finally {
      hideLoader();
    }
    return bContinue;
  }

Page

SizedBox(
                width: double.infinity,
                child: PrimaryButtonBG(
                  text: 'Ricarica wallet',
                  onTap: () {
                    controller.increaseWallet().then((bContinue){
                      openSuccessWallet(context);
                    });
                    
                  },
                ),
              ),

4

Answers


  1. If you want to wait you should use this Future. delay(Duration(Xyz)) or Timer function


    SizedBox(
                width: double.infinity,
                child: PrimaryButtonBG(
                  text: 'Ricarica wallet',
                  onTap: () {
                    Future.delayed(Duration(seconds: XYZ )),
                    controller.increaseWallet().then((bContinue){
                      openSuccessWallet(context);
                    });
                    
                  },
                ),
              ),
    
    Login or Signup to reply.
  2. Easiest way is using workers, for example:

    class WalletController extends GetxController {
      final increasedWallet = false.obs; //your control trigger
    
      @override
      void onInit() {
        super.onInit();
    
        //worker that listening status of RxBool trigger
        ever(increasedWallet, (value) {
          if (value == true) {
    
            ////////
            //place here code for showing bottomSheet
            ////////
    
            increasedWallet.value = false;
          }
        });
      }
      
      Future<bool> increaseWallet() async {
        try {
          await MangoPay.instance.makePayment(
              mangoPayUserId: user!.mangoPayId!,
              mangoPayPaymentWalletId: user!.walletId!,
              cardData: _cardData,
              amount: double.parse(amount),
              paymentCallback: (result, message, transactionId, status) async {
                if (status == "SUCCEEDED") {
                  increasedWallet.value = true; //here trigger turns worker on
                  hideLoader();
                  _bContinue = true;
                }
              });
          
        } catch (e) {
          hideLoader();
          return bContinue;
        } finally {
          hideLoader();
        }
        return bContinue;
      }
    }
    
    Login or Signup to reply.
  3. SizedBox(
                width: double.infinity,
                child: PrimaryButtonBG(
                  text: 'Ricarica wallet',
                  onTap: () async {
                   bool? value = await controller.increaseWallet();
                    if(value != null){
                       openSuccessWallet(context);
                    }
                  },
                ),
              ),
    
    Login or Signup to reply.
  4. if you want to update UI with Getx, there are 2 ways:

    1. Use Obx wrap Widget need update, then when "_bContinue = true;" the UI will update
    2. If you don’t know where to put Obx, you can wrap your whole page with GetBuilder, but this way you have to update you boolean var, ex:
    • In your controller: void showBottomSheet() { _bContinue = true; update(); }
    • Then call function when status == 'SUCCEEDED'
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search