You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Stripe编译报错:ReadableMap无匹配构造函数

Flutter集成Stripe支付编译失败问题及解决方案

问题背景

在Flutter应用中集成Stripe支付功能,流程为:用户点击产品后调用后端生成PaymentIntent并返回前端,前端获取Intent后调用Stripe API收集银行卡信息。但编写前端代码后无法完成编译。

相关代码

main.dart代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// Initialize Stripe
  Stripe.publishableKey = "**********************************************";

.....
.....
.....
}

product.dart代码

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

import '../../services/api.dart';

class ProductsPage extends StatefulWidget {
  const ProductsPage({Key? key}) : super(key: key);

  @override
  State<ProductsPage> createState() => _ProductsPageState();
}

class _ProductsPageState extends State<ProductsPage> {
  String? intentPayment;
  final productsList = [
    {
      "name": "Abonnement 30 jours",
      "description": "Abonnement test 30 jours",
      "price": 100,
      "duree": "30"
    },
    {
      "name": "Abonnement 60 jours",
      "description": "",
      "price": 180,
      "duree": "60"
    },
    {
      "name": "Abonnement 90 jours",
      "description": "Abonnement test 30 jours",
      "price": 250,
      "duree": "90"
    },
  ];

  Future<void> makePayment(product) async {
    try {
      intentPayment = await Api.getIntent(product);
      await Stripe.instance.initPaymentSheet(
          paymentSheetParameters: SetupPaymentSheetParameters(
        paymentIntentClientSecret: intentPayment!,
        // applePay: const PaymentSheetApplePay(merchantCountryCode: 'FR'),
        // googlePay: const PaymentSheetGooglePay(merchantCountryCode: 'FR'),
        style: ThemeMode.dark,
        merchantDisplayName: 'JobMe Test',
      ));
      displayPaymentSheet();
    } catch (e) {
      print("exception: $e");
    }
  }

  displayPaymentSheet() async {
    try {
      await Stripe.instance.presentPaymentSheet().then(((value) {
        showDialog(
            context: context,
            builder: (_) => AlertDialog(
                  content: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Row(
                        children: const [
                          Icon(
                            Icons.check_circle,
                            color: Colors.green,
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Text("Paiement effectué avec succès.")
                        ],
                      ),
                    ],
                  ),
                ));

        intentPayment = null;
      }));
    } on StripeException catch (e) {
      print("Stripe Exception: $e");
    } catch (e) {
      print("exception: $e");
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.white,
            ),
            body: Center(
              child: Column(
                children: [
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[0]);
                      },
                      child: const Text("Abonnement 1 mois. 100€")),
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[1]);
                      },
                      child: const Text("Abonnement 2 mois. 180€")),
                  TextButton(
                      onPressed: () async {
                        await makePayment(productsList[2]);
                      },
                      child: const Text("Abonnement 3 mois. 250€")),
                ],
              ),
            )));
  }
}

编译报错信息

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>)

        super(new HashMap<>());
        ^
    constructor ReadableMap.ReadableMap(JSONObject) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to JSONObject)
    constructor ReadableMap.ReadableMap(Map<String,Object>) is not applicable      (argument mismatch; HashMap<Object,Object> cannot be converted to Map<String,Object>)
1 error

FAILURE: Build failed with an exception.

* What went wrong:Execution failed for task ':stripe_android:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

已尝试的解决操作

  • 删除build文件夹并重新构建
  • 将MainActivity.kt中的FlutterActivity()替换为FlutterFragmentActivity()
  • 执行flutter doctor -v检测,环境无异常

解决方案

这个错误源于stripe_android:5.1.0版本中的构造函数参数不兼容问题,可通过以下步骤解决:

  1. 升级flutter_stripe依赖到最新稳定版
    在项目的pubspec.yaml文件中更新flutter_stripe的版本:

    dependencies:
      flutter_stripe: ^最新稳定版号
    

    执行命令更新依赖:

    flutter pub get
    
  2. 强制指定兼容的stripe_android版本
    如果升级flutter_stripe后问题仍存在,可在pubspec.yaml中添加依赖覆盖:

    dependency_overrides:
      stripe_android: ^6.0.0 # 或更高兼容版本
    

    再次执行flutter pub get更新依赖。

  3. 清理缓存并重新构建

    flutter clean
    flutter pub get
    flutter run
    

内容的提问来源于stack exchange,提问作者Shiroe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:45:31