Yash Trivedi
Yash Trivedi

Reputation: 11

Flutter Does Not Preserve App State Properly During In-App Purchases

Description I am facing a critical issue with Flutter's state preservation when handling in-app purchases. In my app, I have implemented in-app purchases using Provider for state management. The purchase flow works fine in development mode, but in production, I encounter a major problem.

Steps to Reproduce: Open the PurchaseScreen in my Flutter app.

Initiate an in-app purchase (IAP).

The Play Store (Android) or App Store (iOS) purchase dialog appears.

I select Google Pay (GPay) for payment.

The app switches to the Google Pay app for completing the transaction.

After completing the payment, I return to my app.

At this point, the Flutter app restarts instead of preserving the state, making it impossible to track the payment status.

Expected Behavior:

After returning from Google Pay, my app should preserve its state and continue from the purchase screen, allowing me to check the payment status.

Actual Behavior: The Flutter app restarts, losing the previous state, and I cannot retrieve the payment status.

Possible Causes & Observations: It seems Flutter is not preserving the entire state when switching between apps. On Android, this might be due to memory management or activity lifecycle behavior.

On iOS, the app is also restarting, making it impossible to resume the purchase flow properly.

This is a big issue for apps that rely on external payment apps like Google Pay or Apple Pay.

Questions for the Flutter Community: Is there a way to fully preserve the app state when switching between apps? How can we track the payment status reliably after returning to the app? Is this a known issue with Flutter, and are there any workarounds?

I would appreciate any guidance or solutions from the community. Thank you!

Upvotes: 0

Views: 22

Answers (1)

Mastermindsap
Mastermindsap

Reputation: 46

Hello could you share the code snippet of the program that is causing unexpected behaviour. It would help others to understand the problem in a more proper way.
But as far I understand you need to listen to the purchase status, here is the example code from the documentation:

void _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
  purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
    if (purchaseDetails.status == PurchaseStatus.pending) {
      _showPendingUI();
    } else {
      if (purchaseDetails.status == PurchaseStatus.error) {
        _handleError(purchaseDetails.error!);
      } else if (purchaseDetails.status == PurchaseStatus.purchased ||
                 purchaseDetails.status == PurchaseStatus.restored) {
        bool valid = await _verifyPurchase(purchaseDetails);
        if (valid) {
          _deliverProduct(purchaseDetails);
        } else {
          _handleInvalidPurchase(purchaseDetails);
        }
      }
      if (purchaseDetails.pendingCompletePurchase) {
        await InAppPurchase.instance
            .completePurchase(purchaseDetails);
      }
    }
  });
}

(Refer: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase#listening-to-purchase-updates)

Upvotes: 0

Related Questions