Flutter集成PhonePe Payment SDK调用init方法遇MissingPluginException
Flutter集成PhonePe Payment SDK时调用init方法触发MissingPluginException
问题描述
在Flutter应用中集成PhonePe Payment SDK,调用PhonePePaymentSdk.init()方法时触发MissingPluginException异常。
代码片段
Flutter组件代码
import 'dart:convert'; import 'package:crypto/crypto.dart'; import 'package:flutter/material.dart'; import 'package:phonepe_payment_sdk/phonepe_payment_sdk.dart'; class PhonePePaymentScreen extends StatefulWidget { PhonePePaymentScreen(); @override State<PhonePePaymentScreen> createState() => _PhonePePaymentScreenState(); } class _PhonePePaymentScreenState extends State<PhonePePaymentScreen> { @override void initState() { initiatePayment(); body = getCheckSum().toString(); super.initState(); } String environmentValue = "SANDBOX"; String appId = ""; String merchantId = "PGTESTPAYUAT"; bool enableLogging = true; String saltKey = "099eb0cd-02cf-4e2a-8aca-3e6c6aff0399"; String saltIndex = "1"; String callback = "https://webhook.site/ba843c01-f6a6-4076-8fba-24b5719b3f66"; String apiEndPoint = "/pg/v1/pay"; Object result; String body = ""; String checksum = ""; void initiatePayment() async { PhonePePaymentSdk.init(environmentValue, appId, merchantId, enableLogging) .then((val) => { setState(() { result = 'PhonePe SDK Initialized - $val'; }) }) .catchError((error) { handleError(error); return <dynamic>{}; }); } void handleError(error) { setState(() { result = error; }); } void startTransactions() async { PhonePePaymentSdk.startTransaction(body, callback, checksum, "") .then((response) => { setState(() { if (response != null) { String status = response['status'].toString(); String error = response['error'].toString(); if (status == 'SUCCESS') { result = "Flow Completed - Status: Success!"; } else { result = "Flow Completed - Status: $status and Error: $error"; } } else { result = "Flow Incomplete"; } }) }) .catchError((error) { handleError(error); return <dynamic>{}; }); } String getCheckSum() { final reqData = { "merchantId": merchantId, "merchantTransactionId": "MT7850590068188104", "merchantUserId": "MUID123", "amount": 1000, "callbackUrl": callback, "mobileNumber": "9999999999", "paymentInstrument": {"type": "PAY_PAGE"} }; String encoded = base64.encode(utf8.encode(json.encode(reqData))); checksum = '${sha256.convert(utf8.encode(encoded+apiEndPoint+saltKey))}###$saltIndex'; return encoded; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Payment Gateway"), backgroundColor: Color(0xFF009db5), ), body: Column( children: [ Container( child: ElevatedButton( child: Text("Pay Now"), onPressed: () { startTransactions(); }, ), ), Text("$result"), ], ), ); } }
环境与配置信息
- Flutter版本:3.0.0
- Dart版本:2.17.0
pubspec.yaml配置
environment: sdk: ">=2.2.0 <3.0.0" dependencies: crypto: ^3.0.2 phonepe_payment_sdk: ^2.0.1
android/build.gradle配置
allprojects { repositories { google() jcenter() maven { url "https://phonepe.mycloudrepo.io/public/repositories/phonepe-intentsdk-android" } } }
解决方案
MissingPluginException通常是Flutter与原生平台插件通信未正确初始化导致的,按以下步骤排查修复:
修正Dart SDK版本范围
你的Flutter 3.0.0对应Dart 2.17.0,但pubspec里指定的SDK范围是>=2.2.0 <3.0.0,已经超出限制。修改pubspec.yaml的environment部分:environment: sdk: ">=2.17.0 <4.0.0"执行
flutter pub get更新依赖。重新构建原生项目
插件安装后需重新生成原生绑定文件,执行以下命令:flutter clean flutter pub get cd android && ./gradlew clean && cd .. flutter run验证Android端基础配置
确认android/app/build.gradle中minSdkVersion不低于21(PhonePe SDK要求):android { defaultConfig { minSdkVersion 21 // 其他配置... } }若gradle同步失败,手动触发Android项目同步。
检查init方法参数
代码中appId为空字符串,部分SDK版本可能需要有效的沙箱appId,确认并填入PhonePe提供的对应参数。延迟调用init方法
当前在initState中直接调用初始化方法,可能平台通道尚未就绪。改为延迟调用:@override void initState() { super.initState(); Future.delayed(Duration(milliseconds: 500), () { initiatePayment(); }); body = getCheckSum().toString(); }
完成以上步骤后重新运行应用,即可解决MissingPluginException问题。
内容的提问来源于stack exchange,提问作者Vicky
相关产品推荐
相关产品推荐

