It’s my first post here.
I’m struggling with a Stripe integration in a Flutter project and can’t find any other similar issue or problem on the web.
I’m using flutter Bloc and Stripe.
Once I run the function await Stripe.instance.presentPaymentSheet();
and try to fake a payement with the credit card 42424 42424 42424 42424. I have the above error:
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>?'
#0 MethodChannelStripe.presentPaymentSheet
method_channel_stripe.dart:263
<asynchronous suspension>
#1 Stripe.presentPaymentSheet
stripe.dart:517
<asynchronous suspension>
I’m creating the payment intent in my backend and everything seems to be fine with this part in my Stripe Dashboard
Here is a short reproduction of my code:
pubspec.yaml
bloc: ^8.0.0
flutter_bloc: ^8.0.0
flutter_stripe: ^9.3.0
stripe_bloc.dart
class StripeBloc extends Bloc<StripeEvent, StripeState> {
StripeBloc() : super(const StripeState()) {
on<RequestedStripePaymentEvent>(_onRequestedStripePaymentEvent);
}
void _onRequestedStripePaymentEvent(RequestedStripePaymentEvent event,
Emitter<StripeState> emit) async {
try {
final Map<String, dynamic>? paymentIntent = await createPaymentIntent();
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntent?['client_secret'],
));
await Stripe.instance.presentPaymentSheet();
} on StripeException catch (e) {
if (e.error.code == FailureCode.Canceled) {}
if (e.error.code == FailureCode.Failed ||
e.error.code == FailureCode.Timeout) {
}
} finally {
}
}
}
stripe_api.dart
Future<Map<String, dynamic>?> createPaymentIntent() async {
try {
var response =
await postAPIAuthenticated('path');
return jsonDecode(response.body);
} catch (err) {
throw Exception(err);
}
}
Hope guys could help me 🙂 Thanks
I already try to make my code as minimal as possible.
I also try to call the stripe API directly in my front end and still get the same error so I think my backend is not involved in this
3
Answers
It seems that the error is version related.
An issues had just been raised on Github: https://github.com/flutter-stripe/flutter_stripe/issues/1344!
The error means that your response is actually a list and not a map. In other words, the response is like
[ ... ]
and not{ ... }
. Maybe the expected map is actually the first element in the list. If so this maybe can fix it:If this doesn’t fix it I suggest you print the
response
to see what actual response you get and go from thereFor the past 2 days, the problem has been reported numerous times on the GitHub repository of the flutter_stripe package:
https://github.com/flutter-stripe/flutter_stripe/issues/1344
It seems to be due to a problem with version 9.3.0. A downgrade to 9.2.2 solves the problem. For the rest, we’ll probably have to wait for Stripe to resolve it internally.
Hope it helps you !