I am using flutter_stripe 9.4.0, this code is working fine on android device but when I run on IOS it gives an error and app crashes Error is: Could not cast value of type 'NSNull' (0x1b9059de8) to 'NSDictionary' (0x1b9056b58).
I also used Stripe.publishableKey = "*******" in main method as well. and platform :ios, '13.0'
This is my flutter code:
Future<void> makePayment() async {
print("1");
String totalPrice = (_subtotal+_audiototal+_singleAudiototal).toStringAsFixed(2);
int totalPriceAsInt = int.parse(totalPrice.split('.')[0]);
print("2");
try {
paymentIntentData = await createPaymentIntent(totalPriceAsInt.toString(), 'USD');
// Check if cart items are already purchased
final bool itemsAlreadyPurchased = await checkCartItemsPurchased();
print("4");
if (itemsAlreadyPurchased) {
// Show a toast message indicating that items are already purchased
ToastMessage.showMessage("Some items in the cart are already purchased.");
print("5");
} else {
print("6");
print(paymentIntentData);
print(paymentIntentData!['client_secret']);
print(paymentIntentData!['customer']);
print(paymentIntentData!['ephemeralKey']);
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
customFlow: false,
paymentIntentClientSecret: paymentIntentData!['client_secret'],
merchantDisplayName: 'Body and mind',
customerId: paymentIntentData!['customer'],
customerEphemeralKeySecret: paymentIntentData!['ephemeralKey'],
googlePay: null,
applePay: null,
style: ThemeMode.system,
),
);
print("7");
await displayPaymentSheet();
print("8");
if (paymentSuccessful) {
await handlePurchase();
print("9");
}
}
} catch (e) {
print("9");
print("exception error or some-other error: $e");
}
}
Future<bool> checkCartItemsPurchased() async {
final cartController = Get.put(CartController());
final cartCourses = await cartController.getCartCourses();
final cartAudioPackages = await cartController.getCartAudioPackages();
final cartSingleAudio = await cartController.getCartSingleAudios();
if (cartCourses.isNotEmpty || cartAudioPackages.isNotEmpty || cartSingleAudio.isNotEmpty) {
User? user = FirebaseAuth.instance.currentUser;
if (user == null) {
return false;
}
// Check courses
if (cartCourses.isNotEmpty) {
for (final cartCourse in cartCourses) {
// Check if the course is already purchased
final isCoursePurchased = await isItemAlreadyPurchased(cartCourse.id.toString(), 'coursePurchases');
if (isCoursePurchased) {
return true; // Stop and return true if any course is already purchased
}
}
}
// Check audio packages
if (cartAudioPackages.isNotEmpty) {
for (final cartAudioPackage in cartAudioPackages) {
// Check if the audio package is already purchased
final isAudioPurchased = await isItemAlreadyPurchased(cartAudioPackage.id.toString(), 'audioPurchases');
if (isAudioPurchased) {
return true; // Stop and return true if any audio package is already purchased
}
}
}
// check for single audio
if (cartSingleAudio.isNotEmpty) {
for (final cartAudioPackage in cartSingleAudio) {
// Check if the audio package is already purchased
final isAudioPurchased = await isItemAlreadyPurchased(cartAudioPackage.id.toString(), 'single_audio_purchased');
if (isAudioPurchased) {
return true; // Stop and return true if any audio package is already purchased
}
}
}
}
return false; // Return false if no items are already purchased
}
displayPaymentSheet() async {
try {
if (paymentIntentData != null) {
await Stripe.instance.presentPaymentSheet(
options: PaymentSheetPresentOptions(
timeout: 2,
),
);
setState(() {
paymentIntentData = null;
paymentSuccessful = true;
});
} else {
}
} catch (e) {
print("exception display sheet $e");
}
}
createPaymentIntent(String amount,String currency)async{
try{
print("final Amount is");
print(calculateAmount(amount));
Map<String,dynamic> body = {
'amount':calculateAmount(amount),
'currency':currency,
'payment_method_types[]':'card',
};
final response = await http.post(Uri.parse('https://api.stripe.com/v1/payment_intents'),
body: body,
headers: {
'Authorization':'Bearer *********',
'Content-Type':'application/x-www-form-urlencoded',
}
);
return jsonDecode(response.body.toString());
}catch(e){
print("exception error or some-other error: $e");
}
}
calculateAmount(String amount){
final price = int.parse(amount)*100;
return price.toString();
}
Flutter sdk version is: 3.13.1
I solve this error by downgrade flutter_stripe package version. I was using flutter_stripe: 9.4.0 and error solve with flutter_stripe 7.0.0 I am not sure, it will work for every one but it worked for me.