Stripe Flutter:“错误:没有找到适合 ReadableMap(HashMap<Object,Object>) 的合适构造函数”

Shi*_*roe 4 java android stripe-payments build.gradle flutter

我想在我的应用程序中实现 Stripe。

\n

为此,我有以下模式:

\n

用户点击产品,它会链接到后端,后端生成 PaymentIntent 并将其发送到前端。当我的前端收到 Intent 时,他会要求卡、号码和数据来请求 Stripe 的 API。

\n

\n

创建 Front 后,我​​遇到了问题,无法编译我的代码......

\n

所以,这就是我的申请的制作方式:

\n

在 main.dart 中:

\n
void main() async {\n  WidgetsFlutterBinding.ensureInitialized();\n\n  /// Initialize Stripe\n  Stripe.publishableKey = "**********************************************";\n\n.....\n.....\n.....\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在product.dart(产品页面)中:

\n
\nimport 'package:flutter/material.dart';\nimport 'package:flutter_stripe/flutter_stripe.dart';\n\nimport '../../services/api.dart';\n\nclass ProductsPage extends StatefulWidget {\n  const ProductsPage({Key? key}) : super(key: key);\n\n  @override\n  State<ProductsPage> createState() => _ProductsPageState();\n}\n\nclass _ProductsPageState extends State<ProductsPage> {\n  String? intentPayment;\n  final productsList = [\n    {\n      "name": "Abonnement 30 jours",\n      "description": "Abonnement test 30 jours",\n      "price": 100,\n      "duree": "30"\n    },\n    {\n      "name": "Abonnement 60 jours",\n      "description": "",\n      "price": 180,\n      "duree": "60"\n    },\n    {\n      "name": "Abonnement 90 jours",\n      "description": "Abonnement test 30 jours",\n      "price": 250,\n      "duree": "90"\n    },\n  ];\n\n  Future<void> makePayment(product) async {\n    try {\n      intentPayment = await Api.getIntent(product);\n      await Stripe.instance.initPaymentSheet(\n          paymentSheetParameters: SetupPaymentSheetParameters(\n        paymentIntentClientSecret: intentPayment!,\n        // applePay: const PaymentSheetApplePay(merchantCountryCode: 'FR'),\n        // googlePay: const PaymentSheetGooglePay(merchantCountryCode: 'FR'),\n        style: ThemeMode.dark,\n        merchantDisplayName: 'JobMe Test',\n      ));\n      displayPaymentSheet();\n    } catch (e) {\n      print("exception: $e");\n    }\n  }\n\n  displayPaymentSheet() async {\n    try {\n      await Stripe.instance.presentPaymentSheet().then(((value) {\n        showDialog(\n            context: context,\n            builder: (_) => AlertDialog(\n                  content: Column(\n                    mainAxisSize: MainAxisSize.min,\n                    children: [\n                      Row(\n                        children: const [\n                          Icon(\n                            Icons.check_circle,\n                            color: Colors.green,\n                          ),\n                          SizedBox(\n                            width: 10,\n                          ),\n                          Text("Paiement effectu\xc3\xa9 avec succ\xc3\xa8s.")\n                        ],\n                      ),\n                    ],\n                  ),\n                ));\n\n        intentPayment = null;\n      }));\n    } on StripeException catch (e) {\n      print("Stripe Exception: $e");\n    } catch (e) {\n      print("exception: $e");\n    }\n  }\n\n  @override\n  void initState() {\n    super.initState();\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return SafeArea(\n        child: Scaffold(\n            appBar: AppBar(\n              backgroundColor: Colors.white,\n            ),\n            body: Center(\n              child: Column(\n                children: [\n                  TextButton(\n                      onPressed: () async {\n                        await makePayment(productsList[0]);\n                      },\n                      child: const Text("Abonnement 1 mois. 100\xe2\x82\xac")),\n                  TextButton(\n                      onPressed: () async {\n                        await makePayment(productsList[1]);\n                      },\n                      child: const Text("Abonnement 2 mois. 180\xe2\x82\xac")),\n                  TextButton(\n                      onPressed: () async {\n                        await makePayment(productsList[2]);\n                      },\n                      child: const Text("Abonnement 3 mois. 250\xe2\x82\xac")),\n                ],\n              ),\n            )));\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我尝试运行该应用程序时遇到的错误:

\n
C:\\src\\flutter\\.pub-cache\\hosted\\pub.dartlang.org\\stripe_android-5.1.0\\android\\src\\main\\kotlin\\com\\facebook\\react\\bridge\\WritableNativeMap.java:22: error: no suitable constructor found for ReadableMap(HashMap<Object,Object>)\n\n        super(new HashMap<>());\n        ^\n    constructor ReadableMap.ReadableMap(JSONObject) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to JSONObject)\n    constructor ReadableMap.ReadableMap(Map<String,Object>) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to Map<String,Object>)\n1 error\n\nFAILURE: Build failed with an exception.\n\n* What went wrong:Execution failed for task ':stripe_android:compileDebugJavaWithJavac'.\n> Compilation failed; see the compiler error output for details.\n
Run Code Online (Sandbox Code Playgroud)\n

(我的 flutter doctor -v 没问题,我已经尝试删除构建文件夹。我还在 MainActivity.kt 中将 FlutterActivity() 切换为 FlutterFragmentActivity())

\n

请帮忙 :)

\n

小智 8

更新 gradle 版本 android/gradle/wrapper/gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
Run Code Online (Sandbox Code Playgroud)

并将根级别的构建 gradle 更新为 7.2.1android/build.gradle

classpath 'com.android.tools.build:gradle:7.2.1'
Run Code Online (Sandbox Code Playgroud)