flutter_billing 0.0.2 flutter_billing: ^0.0.2 copied to clipboard
A flutter plugin to communicate with billing on iOS and Android.
flutter_billing #
A plugin for Flutter that enables communication with billing API in iOS and Android.
Warning: This plugin is still under development, some billing features are not available yet and testing has been limited. Feedback and Pull Requests are welcome.
Using #
Add flutter_billing
as a dependency in pubspec.yaml
.
Create an instance of the plugin:
final Billing billing = new Billing();
Request available products and details:
final List<BillingProduct> skuDetails = await billing.fetchProducts(<String>[
'my.product.id',
]);
Request purchased products (each purchase is a product id):
final List<String> purchases = await billing.fetchPurchases();
Make a product purchase. It throws in a case of error or returns a list of purchased products on success:
final List<String> purchases = await billing.purchase(productId);
Tips #
Billing issues calls to App Store and Play Store accordingly. e.g. When fetch products are called more than once it may request products from a Store and not cache. To prevent such situations one could use synchronized package and implement similar solution:
class BillingRepository {
final Billing _billing = new Billing();
List<BillingProduct> _cachedProducts;
Future<List<BillingProduct>> getProducts() {
return synchronized(this, () async {
if (_cachedProducts == null) {
_cachedProducts = await _billing.fetchProducts(<String>[
'my.product.id',
]);
}
return _cachedProducts;
});
}
Future<BillingProduct> purchase(BillingProduct product) async {
final List<String> purchases = await _billing.purchase(product.identifier);
// update purchased products
return product;
}
Future<BillingProduct> get(String identifier) async {
final List<BillingProduct> products = await getProducts();
return products.firstWhere((_) => _.identifier == identifier, orElse: () => null);
}
}
Limitations #
This is just an initial version of the plugin. There are still some limitiations:
- iOS implementation is currently under testing
- Only non-consumable in app products are supported