Repository: flutter
Issue: [in-app-purchase][Android] PlatformException(UNAVAILABLE, BillingClient is unset. Try reconnecting., null, null)
Labels: c: crash,platform-android,engine,a: existing-apps,p: in_app_purchase,package,has reproducible steps,P2,found in release: 3.7,found in release: 3.10,team-android,triaged-android

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

***Note***: I haven't tested these steps closely. I just had a big add-to-app flutter app on which it failed like this on my internal testing track (in Google Play Store), I took a look at the plugin code, issues and answers on stackoverflow and it all pointed that it should work, except that in my case it is an add-to-app scenario and I am killing (finish) the flutter activity at some points.

1. Setup an add-to-app flutter app.
2. Kill the Flutter activity and start a native activity.
3. Start the Flutter activity again reusing the flutter engine of the killed activity.
4. Call `await InAppPurchase.instance.isAvailable();` or try to purchase something.

### Expected results

Not to crash

### Actual results

It crashes with `Unhandled Exception: PlatformException(UNAVAILABLE, BillingClient is unset. Try reconnecting., null, null)
`

### Code sample

I am sorry, I didn't have time to create a minimal reproducible example. I'm just attaching the purchase function I am using.

<details><summary>Code sample</summary>

```dart

Future<bool> purchaseSubscriptionPlan(String productId) async {
  // fix from: https://stackoverflow.com/a/71730103/2469583
  final av = await InAppPurchase.instance.isAvailable();
  if (!av) {
    toast("InAppPurchase is not available");
    return false;
  }
  Set<String> kIds = <String>{productId};
  final ProductDetailsResponse response =
      await InAppPurchase.instance.queryProductDetails(kIds);
  if (response.notFoundIDs.isNotEmpty) {
    toast("Products not found: ${response.notFoundIDs}");
    return false;
  }
  List<ProductDetails> products = response.productDetails;
  final ProductDetails productDetails = products[0];
  final PurchaseParam purchaseParam =
      PurchaseParam(productDetails: productDetails);
  InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
  return true;
}
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
04-20 10:15:57.651  1949  2120 V DisplayPowerController[0]: Brightness [0.14998937] reason changing to: 'automatic', previous reason: 'automatic [ dim ]'.
04-20 10:15:57.710 25828 26047 E flutter : [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(UNAVAILABLE, BillingClient is unset. Try reconnecting., null, null)
04-20 10:15:57.710 25828 26047 E flutter : #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653)
04-20 10:15:57.710 25828 26047 E flutter : #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:315)
04-20 10:15:57.710 25828 26047 E flutter : <asynchronous suspension>
04-20 10:15:57.710 25828 26047 E flutter : #2      BillingClient.isReady (package:in_app_purchase_android/src/billing_client_wrappers/billing_client_wrapper.dart:84)
04-20 10:15:57.710 25828 26047 E flutter : <asynchronous suspension>
04-20 10:15:57.710 25828 26047 E flutter : #3      purchaseSubscriptionPlan (package:flutter_module/util/billing/billing.dart:73)
04-20 10:15:57.710 25828 26047 E flutter : <asynchronous suspension>
04-20 10:15:57.710 25828 26047 E flutter : #4      _BillingTestingScreenState.build.<anonymous closure>.<anonymous closure> (package:flutter_module/testing/billing_testing.dart:35)
04-20 10:15:57.710 25828 26047 E flutter : <asynchronous suspension>
04-20 10:15:57.710 25828 26047 E flutter : 
04-20 10:15:59.222  1363 26530 I QC2Comp : [vp9D_20] Stats: Works: Q(25)/Done(19)| Work-Rate: Q(0.0/s Avg=0.0/s) Done(0.000/s Avg=0.009/s)| Stream: 30.30fps 1.2Mbps| Pending(8) i/p-done(0)
04-20 10:15:59.223  1363 26531 I QC2Comp : [vp9D_21] Stats: Works: Q(25)/Done(19)| Work-Rate: Q(0.0/s Avg=0.0/s) Done(0.000/s Avg=0.009/s)| Stream: 30.30fps 500.8Kbps| Pending(8) i/p-done(0)
04-20 10:15:59.502  1363 26565 W C2BqBuffer: last successful dequeue was 2106259802 us ago, 188737 consecutive failures

```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.7.8, on macOS 12.6.1 21G217 darwin-x64, locale
    en-PE)
    • Flutter version 3.7.8 on channel stable at /Users/josue/dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 90c64ed42b (4 weeks ago), 2023-03-21 11:27:08 -0500
    • Engine revision 9aa7816315
    • Dart version 2.19.5
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/josue/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_HOME = /Users/josue/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
      11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.77.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 13
      (API 33) (emulator)
    • macOS (desktop)              • macos         • darwin-x64     • macOS
      12.6.1 21G217 darwin-x64
    • Chrome (web)                 • chrome        • web-javascript • Google
      Chrome 112.0.5615.137

[✓] HTTP Host Availability
    • All required HTTP hosts are available


```

</details>