Skip to content

Instantly share code, notes, and snippets.

@doyle-flutter
Created November 19, 2021 16:39
Show Gist options
  • Save doyle-flutter/3103d71200ddb1c13dfd1d9e4be8e8d3 to your computer and use it in GitHub Desktop.
Save doyle-flutter/3103d71200ddb1c13dfd1d9e4be8e8d3 to your computer and use it in GitHub Desktop.
인앱결제 상품 읽기
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:in_app_purchase_android/in_app_purchase_android.dart';
void main() {
if (defaultTargetPlatform == TargetPlatform.android) {
InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
}
runApp(Sys());
}
class Sys extends StatelessWidget {
const Sys({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => MaterialApp(
onGenerateRoute: (RouteSettings route) => MaterialPageRoute(
settings: RouteSettings(name: MainPage.path),
builder: (context) => MainPage()
),
);
}
class MainPage extends StatefulWidget {
MainPage({Key? key}) : super(key: key);
static const String path = "/";
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final InAppPurchase _inAppPurchase = InAppPurchase.instance;
List<ProductDetails> view = [];
Future fetch() async{
final bool available = await InAppPurchase.instance.isAvailable();
if (available) {
Set<String> ids = Set<String>.from(["product1","2_02202", "1_011", ]);
ProductDetailsResponse res = await _inAppPurchase.queryProductDetails(ids);
print(res.productDetails.toString());
this.view = res.productDetails;
}
if(!mounted) return;
setState(() {});
}
@override
void initState() {
Future(this.fetch);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: this.view.isEmpty
? Center(child: Text("Load"))
: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: this.view.map<Widget>(
(ProductDetails p) => Text("${p.title} : ${p.price}")
).toList(),
),
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment