GILO
GILO

Reputation: 2613

Flutter in_app_purchase '_enablePendingPurchases': enablePendingPurchases() must be called before calling startConnection

I am using the simple code below

bool available = await InAppPurchaseConnection.instance.isAvailable();

however it is returning the error

E/flutter (14525): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:in_app_purchase/src/billing_client_wrappers/billing_client_wrapper.dart': Failed assertion: line 101 pos 12: '_enablePendingPurchases': enablePendingPurchases() must be called before calling startConnection

I was wondering if anyone knew a reason for this error and if so what should i fo about it, Happy to have any suggestions - thanks.

Upvotes: 14

Views: 3545

Answers (2)

PaianuVlad23
PaianuVlad23

Reputation: 1977

I could not import InAppPurchaseConnection to try the accepted solution, and fixed this issue with the following:

import 'package:flutter/foundation.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  // Inform the plugin that this app supports pending purchases on Android.
  // An error will occur on Android if you access the plugin `instance`
  // without this call.
  if (defaultTargetPlatform == TargetPlatform.android) {
    InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
  }
  runApp(MyApp());
}

Upvotes: 1

GILO
GILO

Reputation: 2613

The documentation is very thin on this and should actually be more clear. You need include the line below in main() for it to work.

void main() {
  ///Include this in main() so purchases are enabled
  InAppPurchaseConnection.enablePendingPurchases();

  runApp(MyApp());
}

Upvotes: 58

Related Questions