diff --git a/analysis_options.yaml b/analysis_options.yaml index 4936a8cbd..ab0363a5c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -19,6 +19,7 @@ analyzer: # TODO (albertus-stripe): Remove this once the melos PR is merged since this won't be an issue anymore invalid_dependency: ignore invalid_annotation_target: ignore #https://github.com/rrousselGit/freezed#disabling-invalid_annotation_target-warning-and-warning-in-generates-files + library_private_types_in_public_api: ignore exclude: - "bin/cache/**" - "**/*.freezed.dart" diff --git a/example/integration_test/app_test.dart b/example/integration_test/app_test.dart index b1c862b31..f2a31b090 100644 --- a/example/integration_test/app_test.dart +++ b/example/integration_test/app_test.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter_stripe/flutter_stripe.dart'; @@ -19,9 +20,9 @@ void main() { group('Payment sheet', () { testWidgets('init payment sheet', (_) async { // 1. create payment intent on the server - final _paymentSheetData = await _createTestPaymentSheet(); + final paymentSheetData = await _createTestPaymentSheet(); - expect(_paymentSheetData['paymentIntent'], isNotNull); + expect(paymentSheetData['paymentIntent'], isNotNull); // 2. initialize the payment sheet expect( Stripe.instance.initPaymentSheet( @@ -33,9 +34,9 @@ void main() { ), style: ThemeMode.dark, merchantDisplayName: 'Flutter Stripe Store Demo', - customerId: _paymentSheetData['customer'], - paymentIntentClientSecret: _paymentSheetData['paymentIntent'], - customerEphemeralKeySecret: _paymentSheetData['ephemeralKey'], + customerId: paymentSheetData['customer'], + paymentIntentClientSecret: paymentSheetData['paymentIntent'], + customerEphemeralKeySecret: paymentSheetData['ephemeralKey'], ), ), completes, @@ -90,7 +91,7 @@ void main() { Future> _createTestPaymentSheet() async { // ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' could return multiple IPs, divided by new line - use the last one final ipAddress = kApiUrl.split('\n').last.trim(); - print('IP Address of the server: $ipAddress'); + log('IP Address of the server: $ipAddress'); final url = Uri.parse('http://$ipAddress:4242/payment-sheet'); final response = await http.post( url, diff --git a/example/integration_test/payment_method_test.dart b/example/integration_test/payment_method_test.dart index 7e3f7945f..f3a881fb4 100644 --- a/example/integration_test/payment_method_test.dart +++ b/example/integration_test/payment_method_test.dart @@ -43,7 +43,7 @@ void main() { expirationMonth: 04, expirationYear: 2025, )); - ; + final paymentIntent = await Stripe.instance.confirmPayment( paymentIntentClientSecret: clientSecret['clientSecret'], data: PaymentMethodParams.card( diff --git a/example/lib/main.dart b/example/lib/main.dart index 7622dbdce..c3cea8f09 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,5 @@ +// ignore_for_file: use_key_in_widget_constructors + import 'package:flutter/material.dart'; import 'package:flutter_stripe/flutter_stripe.dart'; import 'package:stripe_example/.env.dart'; @@ -14,7 +16,7 @@ void main() async { } class App extends StatelessWidget { - const App({Key? key}) : super(key: key); + const App({super.key}); @override Widget build(BuildContext context) { diff --git a/example/lib/screens/card_payments/custom_card_payment_screen.dart b/example/lib/screens/card_payments/custom_card_payment_screen.dart index af677d666..efaa371d7 100644 --- a/example/lib/screens/card_payments/custom_card_payment_screen.dart +++ b/example/lib/screens/card_payments/custom_card_payment_screen.dart @@ -7,6 +7,8 @@ import 'package:stripe_example/config.dart'; import 'package:stripe_example/widgets/loading_button.dart'; class CustomCardPaymentScreen extends StatefulWidget { + const CustomCardPaymentScreen({super.key}); + @override _CustomCardPaymentScreenState createState() => _CustomCardPaymentScreenState(); @@ -120,6 +122,8 @@ class _CustomCardPaymentScreenState extends State { } Future _handlePayPress() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); + await Stripe.instance.dangerouslyUpdateCardDetails(_card); try { @@ -158,16 +162,18 @@ class _CustomCardPaymentScreenState extends State { if (paymentIntentResult['error'] != null) { // Error during creating or confirming Intent - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: ${paymentIntentResult['error']}'))); - return; + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + return; + } } if (paymentIntentResult['clientSecret'] != null && paymentIntentResult['requiresAction'] == null) { // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( + scaffoldMessenger.showSnackBar(SnackBar( content: Text('Success!: The payment was confirmed successfully!'))); return; @@ -184,25 +190,27 @@ class _CustomCardPaymentScreenState extends State { await confirmIntent(paymentIntent.id); } else { // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Error: ${paymentIntentResult['error']}'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + } } } } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); rethrow; } } Future confirmIntent(String paymentIntentId) async { + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await callNoWebhookPayEndpointIntentId( paymentIntentId: paymentIntentId); - if (result['error'] != null) { - ScaffoldMessenger.of(context) + if (result['error'] != null && context.mounted) { + scaffoldMessenger .showSnackBar(SnackBar(content: Text('Error: ${result['error']}'))); } else { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( + scaffoldMessenger.showSnackBar(SnackBar( content: Text('Success!: The payment was confirmed successfully!'))); } } diff --git a/example/lib/screens/card_payments/no_webhook_payment_cardform_screen.dart b/example/lib/screens/card_payments/no_webhook_payment_cardform_screen.dart index a4bb24e66..21313e86c 100644 --- a/example/lib/screens/card_payments/no_webhook_payment_cardform_screen.dart +++ b/example/lib/screens/card_payments/no_webhook_payment_cardform_screen.dart @@ -10,6 +10,8 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class NoWebhookPaymentCardFormScreen extends StatefulWidget { + const NoWebhookPaymentCardFormScreen({super.key}); + @override _NoWebhookPaymentCardFormScreenState createState() => _NoWebhookPaymentCardFormScreenState(); @@ -86,6 +88,8 @@ class _NoWebhookPaymentCardFormScreenState return; } + final scaffoldMessenger = ScaffoldMessenger.of(context); + try { // 1. Gather customer billing information (ex. email) @@ -120,8 +124,10 @@ class _NoWebhookPaymentCardFormScreenState if (paymentIntentResult['error'] != null) { // Error during creating or confirming Intent - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: ${paymentIntentResult['error']}'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + } return; } @@ -129,10 +135,12 @@ class _NoWebhookPaymentCardFormScreenState paymentIntentResult['requiresAction'] == null) { // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: - Text('Success!: The payment was confirmed successfully!'))); - return; + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: + Text('Success!: The payment was confirmed successfully!'))); + return; + } } if (paymentIntentResult['clientSecret'] != null && @@ -153,27 +161,34 @@ class _NoWebhookPaymentCardFormScreenState // 5. Call API to confirm intent await confirmIntent(paymentIntent.id); } else { - // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Error: ${paymentIntentResult['error']}'))); + if (context.mounted) { + // Payment succedeed + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + } } } } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future confirmIntent(String paymentIntentId) async { + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await callNoWebhookPayEndpointIntentId( paymentIntentId: paymentIntentId); - if (result['error'] != null) { - ScaffoldMessenger.of(context) + if (result['error'] != null && context.mounted) { + scaffoldMessenger .showSnackBar(SnackBar(content: Text('Error: ${result['error']}'))); } else { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success!: The payment was confirmed successfully!'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: + Text('Success!: The payment was confirmed successfully!'))); + } } } diff --git a/example/lib/screens/card_payments/no_webhook_payment_screen.dart b/example/lib/screens/card_payments/no_webhook_payment_screen.dart index e6b8ae45f..e2c1bdafb 100644 --- a/example/lib/screens/card_payments/no_webhook_payment_screen.dart +++ b/example/lib/screens/card_payments/no_webhook_payment_screen.dart @@ -10,6 +10,8 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class NoWebhookPaymentScreen extends StatefulWidget { + const NoWebhookPaymentScreen({super.key}); + @override _NoWebhookPaymentScreenState createState() => _NoWebhookPaymentScreenState(); } @@ -85,6 +87,8 @@ class _NoWebhookPaymentScreenState extends State { return; } + final scaffoldMessenger = ScaffoldMessenger.of(context); + try { // 1. Gather customer billing information (ex. email) final billingDetails = BillingDetails( @@ -118,16 +122,19 @@ class _NoWebhookPaymentScreenState extends State { if (paymentIntentResult['error'] != null) { // Error during creating or confirming Intent - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: ${paymentIntentResult['error']}'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + } return; } if (paymentIntentResult['clientSecret'] != null && - paymentIntentResult['requiresAction'] == null) { + paymentIntentResult['requiresAction'] == null && + context.mounted) { // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( + scaffoldMessenger.showSnackBar(SnackBar( content: Text('Success!: The payment was confirmed successfully!'))); return; @@ -146,26 +153,33 @@ class _NoWebhookPaymentScreenState extends State { await confirmIntent(paymentIntent.id); } else { // Payment succedeed - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Error: ${paymentIntentResult['error']}'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Error: ${paymentIntentResult['error']}'))); + } } } } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future confirmIntent(String paymentIntentId) async { + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await callNoWebhookPayEndpointIntentId( paymentIntentId: paymentIntentId); - if (result['error'] != null) { - ScaffoldMessenger.of(context) + if (result['error'] != null && context.mounted) { + scaffoldMessenger .showSnackBar(SnackBar(content: Text('Error: ${result['error']}'))); } else { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success!: The payment was confirmed successfully!'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: + Text('Success!: The payment was confirmed successfully!'))); + } } } diff --git a/example/lib/screens/card_payments/webhook_payment_screen.dart b/example/lib/screens/card_payments/webhook_payment_screen.dart index 89842025a..c15c3aad5 100644 --- a/example/lib/screens/card_payments/webhook_payment_screen.dart +++ b/example/lib/screens/card_payments/webhook_payment_screen.dart @@ -10,6 +10,8 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class WebhookPaymentScreen extends StatefulWidget { + const WebhookPaymentScreen({super.key}); + @override _WebhookPaymentScreenState createState() => _WebhookPaymentScreenState(); } @@ -71,6 +73,7 @@ class _WebhookPaymentScreenState extends State { } Future _handlePayPress() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); if (_card == null) { return; } @@ -107,9 +110,10 @@ class _WebhookPaymentScreenState extends State { _saveCard == true ? PaymentIntentsFutureUsage.OffSession : null, ), ); - - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success!: The payment was confirmed successfully!'))); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Success!: The payment was confirmed successfully!'))); + } } Future> fetchPaymentIntentClientSecret() async { diff --git a/example/lib/screens/customer_sheet/customer_sheet_screen.dart b/example/lib/screens/customer_sheet/customer_sheet_screen.dart index d49262e23..d6f9a9cb5 100644 --- a/example/lib/screens/customer_sheet/customer_sheet_screen.dart +++ b/example/lib/screens/customer_sheet/customer_sheet_screen.dart @@ -8,6 +8,8 @@ import 'package:stripe_example/widgets/example_scaffold.dart'; import 'package:stripe_example/widgets/loading_button.dart'; class CustomerSheetScreen extends StatefulWidget { + const CustomerSheetScreen({super.key}); + @override _CustomerSheetScreenState createState() => _CustomerSheetScreenState(); } @@ -64,6 +66,7 @@ class _CustomerSheetScreenState extends State { } Future initCustomerSheet() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 1. retrieve customer from backend. final data = await _createTestCustomerSheet(); @@ -101,14 +104,17 @@ class _CustomerSheetScreenState extends State { step = 1; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: $e')), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar(content: Text('Error: $e')), + ); + } rethrow; } } Future confirmCustomerSheet() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 3. display the customer sheet. final result = await Stripe.instance.presentCustomerSheet(); @@ -117,25 +123,31 @@ class _CustomerSheetScreenState extends State { step = 0; }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - 'Payment preferences modfied completed option selected: ${result?.paymentOption?.label}}'), - ), - ); - } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Error from Stripe: ${e.error.localizedMessage}'), + content: Text( + 'Payment preferences modfied completed option selected: ${result?.paymentOption?.label}}'), ), ); + } + } on Exception catch (e) { + if (e is StripeException) { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Error from Stripe: ${e.error.localizedMessage}'), + ), + ); + } } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Unforeseen error: ${e}'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: $e'), + ), + ); + } } } } diff --git a/example/lib/screens/financial_connections.dart/financial_connections_session_screen.dart b/example/lib/screens/financial_connections.dart/financial_connections_session_screen.dart index 6f2df02b4..7b59d2872 100644 --- a/example/lib/screens/financial_connections.dart/financial_connections_session_screen.dart +++ b/example/lib/screens/financial_connections.dart/financial_connections_session_screen.dart @@ -10,7 +10,7 @@ import '../../config.dart'; import '../../widgets/response_card.dart'; class FinancialConnectionsScreen extends StatefulWidget { - const FinancialConnectionsScreen({Key? key}) : super(key: key); + const FinancialConnectionsScreen({super.key}); @override State createState() => @@ -40,32 +40,31 @@ class _FinancialConnectionsScreenState } Future _collectAccount(BuildContext context) async { - // Precondition: - // 1. Make sure to create a financial connection session on the backend and - // forward the client secret of the session to the app. + final messenger = ScaffoldMessenger.of(context); final result = await _financialConnectionsSheet(); final clientSecret = await result['clientSecret']; - // 2. use the client secret to confirm the payment and handle the result. try { final result = await Stripe.instance.collectFinancialConnectionsAccounts( clientSecret: clientSecret, ); + if (!mounted) return; setState(() { response = result.toString(); }); } on Exception catch (e) { + if (!mounted) return; if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + messenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( + messenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } @@ -73,32 +72,31 @@ class _FinancialConnectionsScreenState } Future _collectBankToken(BuildContext context) async { - // Precondition: - // 1. Make sure to create a financial connection session on the backend and - // forward the client secret of the session to the app. + final messenger = ScaffoldMessenger.of(context); final result = await _financialConnectionsSheet(); final clientSecret = await result['clientSecret']; - // 2. use the client secret to confirm the payment and handle the result. try { final result = await Stripe.instance.collectBankAccountToken( clientSecret: clientSecret, ); + if (!mounted) return; setState(() { response = result.toString(); }); } on Exception catch (e) { + if (!mounted) return; if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + messenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( + messenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/others/can_add_to_wallet_screen.dart b/example/lib/screens/others/can_add_to_wallet_screen.dart index 96814f67b..ab204f3a8 100644 --- a/example/lib/screens/others/can_add_to_wallet_screen.dart +++ b/example/lib/screens/others/can_add_to_wallet_screen.dart @@ -6,11 +6,13 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class CanAddToWalletScreen extends StatefulWidget { + const CanAddToWalletScreen({super.key}); + @override - _CanAddToWalletScreenState createState() => _CanAddToWalletScreenState(); + CanAddToWalletScreenState createState() => CanAddToWalletScreenState(); } -class _CanAddToWalletScreenState extends State { +class CanAddToWalletScreenState extends State { late TextEditingController _controller; CanAddCardToWalletResult? canAddCardToWallet; IsCardInWalletResult? isCardInWallet; @@ -73,33 +75,42 @@ class _CanAddToWalletScreenState extends State { } Future _handleCanAddToWallet(String last4) async { + final scaffoldManager = ScaffoldMessenger.of(context); + try { final result = await Stripe.instance.canAddCardToWallet( CanAddCardToWalletParams( cardLastFour: last4, ), ); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Can add card to wallet: ${result}')), - ); + if (mounted) { + scaffoldManager.showSnackBar( + SnackBar(content: Text('Can add card to wallet: $result')), + ); + } return result; } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (mounted) { + scaffoldManager.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future _isCardInToWallet(String last4) async { + final scaffoldManager = ScaffoldMessenger.of(context); try { final result = await Stripe.instance.isCardInWallet(last4); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Can add card to wallet: ${result}')), - ); + if (mounted) { + scaffoldManager.showSnackBar( + SnackBar(content: Text('Is card in wallet: $result')), + ); + } return result; } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (mounted) { + scaffoldManager.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } diff --git a/example/lib/screens/others/cvc_re_collection_screen.dart b/example/lib/screens/others/cvc_re_collection_screen.dart index 7e9560219..56dd8e467 100644 --- a/example/lib/screens/others/cvc_re_collection_screen.dart +++ b/example/lib/screens/others/cvc_re_collection_screen.dart @@ -9,11 +9,13 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class CVCReCollectionScreen extends StatefulWidget { + const CVCReCollectionScreen({super.key}); + @override - _CVCReCollectionScreenState createState() => _CVCReCollectionScreenState(); + CVCReCollectionScreenState createState() => CVCReCollectionScreenState(); } -class _CVCReCollectionScreenState extends State { +class CVCReCollectionScreenState extends State { String _email = 'email@stripe.com'; String _cvc = ''; @@ -62,15 +64,18 @@ class _CVCReCollectionScreenState extends State { } // 1. fetch Intent Client Secret from backend - final paymentMethod = await _fetchPaymentIntentWithPaymentMethod(); + final scaffoldMessenger = ScaffoldMessenger.of(context); + final paymentMethod = await _fetchPaymentIntentWithPaymentMethod(); if (paymentMethod['error'] != null) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error code: ${paymentMethod['error']}'))); + if (mounted) { + scaffoldMessenger.showSnackBar( + SnackBar(content: Text('Error code: ${paymentMethod['error']}'))); + } return; } - ScaffoldMessenger.of(context).showSnackBar(SnackBar( + scaffoldMessenger.showSnackBar(SnackBar( content: Text('Success!: The payment was confirmed successfully!'))); } @@ -78,17 +83,23 @@ class _CVCReCollectionScreenState extends State { if (_cvc.isEmpty || _email.isEmpty) { return; } + final scaffoldMessenger = ScaffoldMessenger.of(context); final paymentIntent = await _callChargeCardOffSession( email: _email, ); log('paymentIntent $paymentIntent'); if (paymentIntent['error'] != null) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error code: ${paymentIntent['error']}'))); + if (mounted) { + scaffoldMessenger.showSnackBar( + SnackBar(content: Text('Error code: ${paymentIntent['error']}'))); + } } else if (paymentIntent['succeeded'] == true) { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success!: The payment was confirmed successfully!'))); + if (mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: + Text('Success!: The payment was confirmed successfully!'))); + } } else { // Handle other statuses accordingly throw UnimplementedError(); diff --git a/example/lib/screens/others/legacy_token_bank_screen.dart b/example/lib/screens/others/legacy_token_bank_screen.dart index 9b198df91..79e46d6b1 100644 --- a/example/lib/screens/others/legacy_token_bank_screen.dart +++ b/example/lib/screens/others/legacy_token_bank_screen.dart @@ -6,11 +6,13 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class LegacyTokenBankScreen extends StatefulWidget { + const LegacyTokenBankScreen({super.key}); + @override - _LegacyTokenBankScreenState createState() => _LegacyTokenBankScreenState(); + LegacyTokenBankScreenState createState() => LegacyTokenBankScreenState(); } -class _LegacyTokenBankScreenState extends State { +class LegacyTokenBankScreenState extends State { late TextEditingController _controller; TokenData? tokenData; @@ -55,6 +57,7 @@ class _LegacyTokenBankScreenState extends State { } Future _handleCreateTokenPress() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 1. Gather customer billing information (ex. email) final params = BankAccountTokenParams( @@ -71,12 +74,15 @@ class _LegacyTokenBankScreenState extends State { setState(() { this.tokenData = tokenData; }); - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success: The token was created successfully!'))); + if (mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Success: The token was created successfully!'))); + } return; } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } diff --git a/example/lib/screens/others/legacy_token_card_screen.dart b/example/lib/screens/others/legacy_token_card_screen.dart index 36e08981b..0aa1ec55a 100644 --- a/example/lib/screens/others/legacy_token_card_screen.dart +++ b/example/lib/screens/others/legacy_token_card_screen.dart @@ -6,11 +6,13 @@ import 'package:stripe_example/widgets/loading_button.dart'; import 'package:stripe_example/widgets/response_card.dart'; class LegacyTokenCardScreen extends StatefulWidget { + const LegacyTokenCardScreen({super.key}); + @override - _LegacyTokenCardScreenState createState() => _LegacyTokenCardScreenState(); + LegacyTokenCardScreenState createState() => LegacyTokenCardScreenState(); } -class _LegacyTokenCardScreenState extends State { +class LegacyTokenCardScreenState extends State { CardFieldInputDetails? _card; TokenData? tokenData; @@ -48,6 +50,7 @@ class _LegacyTokenCardScreenState extends State { if (_card == null) { return; } + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 1. Gather customer billing information (ex. email) @@ -67,12 +70,15 @@ class _LegacyTokenCardScreenState extends State { setState(() { this.tokenData = tokenData; }); - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('Success: The token was created successfully!'))); + if (mounted) { + scaffoldMessenger.showSnackBar(SnackBar( + content: Text('Success: The token was created successfully!'))); + } return; } catch (e) { - ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text('Error: $e'))); + if (mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } diff --git a/example/lib/screens/payment_sheet/express_checkout/express_checkout_element.dart b/example/lib/screens/payment_sheet/express_checkout/express_checkout_element.dart index 6a2682e3f..62ee97d9e 100644 --- a/example/lib/screens/payment_sheet/express_checkout/express_checkout_element.dart +++ b/example/lib/screens/payment_sheet/express_checkout/express_checkout_element.dart @@ -9,11 +9,13 @@ import 'platforms/express_checkout_element.dart' if (dart.library.js) 'platforms/express_checkout_element_web.dart'; class ExpressCheckoutElementExample extends StatefulWidget { + const ExpressCheckoutElementExample({super.key}); + @override - _ThemeCardExampleState createState() => _ThemeCardExampleState(); + ThemeCardExampleState createState() => ThemeCardExampleState(); } -class _ThemeCardExampleState extends State { +class ThemeCardExampleState extends State { String? clientSecret; @override @@ -23,19 +25,22 @@ class _ThemeCardExampleState extends State { } Future getClientSecret() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { final client = await createPaymentIntent(); setState(() { clientSecret = client; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - e.toString(), + if (mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text( + e.toString(), + ), ), - ), - ); + ); + } } } diff --git a/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element.dart b/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element.dart index 33c7c24e7..d1c7b657e 100644 --- a/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element.dart +++ b/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element.dart @@ -5,7 +5,7 @@ Future pay() async { } class ExpressCheckoutWidget extends StatelessWidget { - const ExpressCheckoutWidget(this.clientSecret); + const ExpressCheckoutWidget(this.clientSecret, {super.key}); final String? clientSecret; diff --git a/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element_web.dart b/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element_web.dart index c4dac9ad0..57c4a9d00 100644 --- a/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element_web.dart +++ b/example/lib/screens/payment_sheet/express_checkout/platforms/express_checkout_element_web.dart @@ -15,7 +15,7 @@ Future pay() async { } class ExpressCheckoutWidget extends StatelessWidget { - const ExpressCheckoutWidget(this.clientSecret); + const ExpressCheckoutWidget(this.clientSecret, {super.key}); final String? clientSecret; diff --git a/example/lib/screens/payment_sheet/payment_element/payment_element.dart b/example/lib/screens/payment_sheet/payment_element/payment_element.dart index e316bdc6c..657be7c07 100644 --- a/example/lib/screens/payment_sheet/payment_element/payment_element.dart +++ b/example/lib/screens/payment_sheet/payment_element/payment_element.dart @@ -1,14 +1,16 @@ import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; import 'package:stripe_example/config.dart'; +import 'package:stripe_example/widgets/loading_button.dart'; -import 'package:http/http.dart' as http; import 'platforms/payment_element.dart' if (dart.library.js) 'platforms/payment_element_web.dart'; -import 'package:stripe_example/widgets/loading_button.dart'; class PaymentElementExample extends StatefulWidget { + const PaymentElementExample({super.key}); + @override _ThemeCardExampleState createState() => _ThemeCardExampleState(); } @@ -23,13 +25,14 @@ class _ThemeCardExampleState extends State { } Future getClientSecret() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { final client = await createPaymentIntent(); setState(() { clientSecret = client; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( content: Text( e.toString(), diff --git a/example/lib/screens/payment_sheet/payment_element/platforms/payment_element.dart b/example/lib/screens/payment_sheet/payment_element/platforms/payment_element.dart index 9818aa3ec..97aa852ef 100644 --- a/example/lib/screens/payment_sheet/payment_element/platforms/payment_element.dart +++ b/example/lib/screens/payment_sheet/payment_element/platforms/payment_element.dart @@ -5,7 +5,7 @@ Future pay() async { } class PlatformPaymentElement extends StatelessWidget { - const PlatformPaymentElement(this.clientSecret); + const PlatformPaymentElement(this.clientSecret, {super.key}); final String? clientSecret; diff --git a/example/lib/screens/payment_sheet/payment_element/platforms/payment_element_web.dart b/example/lib/screens/payment_sheet/payment_element/platforms/payment_element_web.dart index ca2bdcaea..48621bc77 100644 --- a/example/lib/screens/payment_sheet/payment_element/platforms/payment_element_web.dart +++ b/example/lib/screens/payment_sheet/payment_element/platforms/payment_element_web.dart @@ -15,7 +15,7 @@ Future pay() async { } class PlatformPaymentElement extends StatelessWidget { - const PlatformPaymentElement(this.clientSecret); + const PlatformPaymentElement(this.clientSecret, {super.key}); final String? clientSecret; diff --git a/example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart b/example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart index 96e93121b..933cdc296 100644 --- a/example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart +++ b/example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart @@ -9,13 +9,15 @@ import 'package:stripe_example/widgets/example_scaffold.dart'; import 'package:stripe_example/widgets/loading_button.dart'; class PaymentSheetDefferedScreen extends StatefulWidget { + const PaymentSheetDefferedScreen({super.key}); + @override _PaymentSheetScreenState createState() => _PaymentSheetScreenState(); } class _PaymentSheetScreenState extends State { int step = 0; - _PaymentMode? mode = null; + _PaymentMode? mode; @override Widget build(BuildContext context) { @@ -122,6 +124,7 @@ class _PaymentSheetScreenState extends State { } Future initPaymentSheetPaymentMode() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // // 1. create payment intent on the server // final data = await _createTestPaymentSheet(); @@ -195,14 +198,15 @@ class _PaymentSheetScreenState extends State { step = 2; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: $e')), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future initPaymentSheetSetupMode() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // // 1. create payment intent on the server // final data = await _createTestPaymentSheet(); @@ -276,14 +280,15 @@ class _PaymentSheetScreenState extends State { step = 2; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: $e')), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future confirmPayment() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 3. display the payment sheet. await Stripe.instance.presentPaymentSheet(); @@ -291,25 +296,28 @@ class _PaymentSheetScreenState extends State { setState(() { step = 0; }); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } } on Exception catch (e) { if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Unforeseen error: ${e}'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: $e'), + ), + ); + } } } } diff --git a/example/lib/screens/payment_sheet/payment_sheet_screen.dart b/example/lib/screens/payment_sheet/payment_sheet_screen.dart index b3ef7244b..d8540187f 100644 --- a/example/lib/screens/payment_sheet/payment_sheet_screen.dart +++ b/example/lib/screens/payment_sheet/payment_sheet_screen.dart @@ -9,6 +9,8 @@ import 'package:stripe_example/widgets/example_scaffold.dart'; import 'package:stripe_example/widgets/loading_button.dart'; class PaymentSheetScreen extends StatefulWidget { + const PaymentSheetScreen({super.key}); + @override _PaymentSheetScreenState createState() => _PaymentSheetScreenState(); } @@ -65,6 +67,7 @@ class _PaymentSheetScreenState extends State { } Future initPaymentSheet() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 1. create payment intent on the server final data = await _createTestPaymentSheet(); @@ -135,14 +138,15 @@ class _PaymentSheetScreenState extends State { step = 1; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: $e')), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar(SnackBar(content: Text('Error: $e'))); + } rethrow; } } Future confirmPayment() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 3. display the payment sheet. await Stripe.instance.presentPaymentSheet(); @@ -151,24 +155,30 @@ class _PaymentSheetScreenState extends State { step = 0; }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); - } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Error from Stripe: ${e.error.localizedMessage}'), + content: Text('Payment succesfully completed'), ), ); + } + } on Exception catch (e) { + if (e is StripeException) { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Error from Stripe: ${e.error.localizedMessage}'), + ), + ); + } } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Unforeseen error: ${e}'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: $e'), + ), + ); + } } } } diff --git a/example/lib/screens/payment_sheet/payment_sheet_screen_custom_flow.dart b/example/lib/screens/payment_sheet/payment_sheet_screen_custom_flow.dart index f4de7cf98..2827442c9 100644 --- a/example/lib/screens/payment_sheet/payment_sheet_screen_custom_flow.dart +++ b/example/lib/screens/payment_sheet/payment_sheet_screen_custom_flow.dart @@ -3,12 +3,13 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_stripe/flutter_stripe.dart'; import 'package:http/http.dart' as http; +import 'package:stripe_example/config.dart'; import 'package:stripe_example/widgets/example_scaffold.dart'; import 'package:stripe_example/widgets/loading_button.dart'; -import 'package:stripe_example/config.dart'; - class PaymentSheetScreenWithCustomFlow extends StatefulWidget { + const PaymentSheetScreenWithCustomFlow({super.key}); + @override _PaymentSheetScreenState createState() => _PaymentSheetScreenState(); } @@ -54,6 +55,7 @@ class _PaymentSheetScreenState extends State { } Future initPaymentSheet() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 1. create payment intent on the server final data = await createTestPaymentSheet(); @@ -81,14 +83,17 @@ class _PaymentSheetScreenState extends State { step = 1; }); } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error: $e')), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar(content: Text('Error: $e')), + ); + } rethrow; } } Future presentPaymentSheet() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 3. display the payment sheet. await Stripe.instance.presentPaymentSheet(); @@ -97,29 +102,36 @@ class _PaymentSheetScreenState extends State { step = 2; }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment option selected'), - ), - ); - } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Error from Stripe: ${e.error.localizedMessage}'), - ), - ); - } else { - ScaffoldMessenger.of(context).showSnackBar( + if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Payment option selected'), ), ); } + } on Exception catch (e) { + if (e is StripeException) { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Error from Stripe: ${e.error.localizedMessage}'), + ), + ); + } else { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: ${e}'), + ), + ); + } + } + } } } Future confirmPayment() async { + final scaffoldMessenger = ScaffoldMessenger.of(context); try { // 4. Confirm the payment sheet. await Stripe.instance.confirmPaymentSheetPayment(); @@ -128,24 +140,30 @@ class _PaymentSheetScreenState extends State { step = 0; }); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); - } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Error from Stripe: ${e.error.localizedMessage}'), + content: Text('Payment succesfully completed'), ), ); + } + } on Exception catch (e) { + if (e is StripeException) { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Error from Stripe: ${e.error.localizedMessage}'), + ), + ); + } } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Unforeseen error: ${e}'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: ${e}'), + ), + ); + } } } } diff --git a/example/lib/screens/regional_payment_methods/affirm_screen.dart b/example/lib/screens/regional_payment_methods/affirm_screen.dart index e9de87566..e832ac329 100644 --- a/example/lib/screens/regional_payment_methods/affirm_screen.dart +++ b/example/lib/screens/regional_payment_methods/affirm_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class AffirmScreen extends StatelessWidget { - const AffirmScreen({Key? key}) : super(key: key); + const AffirmScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -33,6 +33,7 @@ class AffirmScreen extends StatelessWidget { // see file main.dart in this example app. // 1. on the backend create a payment intent for payment method and save the // client secret. + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await _createPaymentIntent(); final clientSecret = await result['clientSecret']; @@ -43,26 +44,29 @@ class AffirmScreen extends StatelessWidget { data: PaymentMethodParams.affirm(paymentMethodData: PaymentMethodData()), ); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (e is StripeException && context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( content: Text( 'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Unforeseen error: ${e}'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: $e'), + ), + ); + } } } } diff --git a/example/lib/screens/regional_payment_methods/ali_pay_screen.dart b/example/lib/screens/regional_payment_methods/ali_pay_screen.dart index 620461e98..1cc1eb6e7 100644 --- a/example/lib/screens/regional_payment_methods/ali_pay_screen.dart +++ b/example/lib/screens/regional_payment_methods/ali_pay_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class AliPayScreen extends StatelessWidget { - const AliPayScreen({Key? key}) : super(key: key); + const AliPayScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -34,6 +34,7 @@ class AliPayScreen extends StatelessWidget { // see file main.dart in this example app. // 1. on the backend create a payment intent for payment method and save the // client secret. + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await _createPaymentIntent(); final clientSecret = await result['clientSecret']; @@ -45,25 +46,28 @@ class AliPayScreen extends StatelessWidget { paymentMethodData: const PaymentMethodData(), ), ); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); - } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Error from Stripe: ${e.error.localizedMessage}'), + content: Text('Payment succesfully completed'), ), ); - } else { - ScaffoldMessenger.of(context).showSnackBar( + } + } on Exception catch (e) { + if (e is StripeException && context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); + } else { + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Unforeseen error: $e'), + ), + ); + } } } } diff --git a/example/lib/screens/regional_payment_methods/aubecs_debit.dart b/example/lib/screens/regional_payment_methods/aubecs_debit.dart index dbcf2b9af..48aaf687f 100644 --- a/example/lib/screens/regional_payment_methods/aubecs_debit.dart +++ b/example/lib/screens/regional_payment_methods/aubecs_debit.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class AubecsExample extends StatefulWidget { - const AubecsExample({Key? key}) : super(key: key); + const AubecsExample({super.key}); @override State createState() => _AubecsExampleState(); @@ -89,6 +89,7 @@ class _AubecsExampleState extends State { } Future _pay(BuildContext context) async { + final scaffoldMessenger = ScaffoldMessenger.of(context); // Precondition: //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK // see file main.dart in this example app. @@ -105,23 +106,24 @@ class _AubecsExampleState extends State { paymentMethodData: PaymentMethodDataAubecs(formDetails: _details!), ), ); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (e is StripeException && context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); - } else { - ScaffoldMessenger.of(context).showSnackBar( + } else if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/regional_payment_methods/cash_app_screen.dart b/example/lib/screens/regional_payment_methods/cash_app_screen.dart index 44e6e805d..4ea0684c6 100644 --- a/example/lib/screens/regional_payment_methods/cash_app_screen.dart +++ b/example/lib/screens/regional_payment_methods/cash_app_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class CashAppScreen extends StatelessWidget { - const CashAppScreen({Key? key}) : super(key: key); + const CashAppScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -29,16 +29,17 @@ class CashAppScreen extends StatelessWidget { } Future _pay(BuildContext context) async { - // Precondition: - //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK - // see file main.dart in this example app. - // 1. on the backend create a payment intent for payment method and save the - // client secret. - final result = await _createPaymentIntent(); - final clientSecret = await result['clientSecret']; - - // 2. use the client secret to confirm the payment and handle the result. + final scaffoldMessenger = ScaffoldMessenger.of(context); try { + // Precondition: + //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK + // see file main.dart in this example app. + // 1. on the backend create a payment intent for payment method and save the + // client secret. + final result = await _createPaymentIntent(); + final clientSecret = result['clientSecret']; + + // 2. use the client secret to confirm the payment and handle the result. await Stripe.instance.confirmPayment( paymentIntentClientSecret: clientSecret, data: PaymentMethodParams.cashAppPay( @@ -46,23 +47,25 @@ class CashAppScreen extends StatelessWidget { ), ); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( + if (!context.mounted) return; + scaffoldMessenger.showSnackBar( + const SnackBar( content: Text('Payment succesfully completed'), ), ); } on Exception catch (e) { + if (!context.mounted) return; if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( content: Text( 'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/regional_payment_methods/fpx_screen.dart b/example/lib/screens/regional_payment_methods/fpx_screen.dart index 43c9e68e5..69eefa8b2 100644 --- a/example/lib/screens/regional_payment_methods/fpx_screen.dart +++ b/example/lib/screens/regional_payment_methods/fpx_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class FpxScreen extends StatelessWidget { - const FpxScreen({Key? key}) : super(key: key); + const FpxScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -29,6 +29,7 @@ class FpxScreen extends StatelessWidget { } Future _pay(BuildContext context) async { + final scaffoldMessenger = ScaffoldMessenger.of(context); // Precondition: //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK // see file main.dart in this example app. @@ -47,23 +48,24 @@ class FpxScreen extends StatelessWidget { ), ), ); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (e is StripeException && context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/regional_payment_methods/grab_pay_screen.dart b/example/lib/screens/regional_payment_methods/grab_pay_screen.dart index 6cc65d4d9..669820639 100644 --- a/example/lib/screens/regional_payment_methods/grab_pay_screen.dart +++ b/example/lib/screens/regional_payment_methods/grab_pay_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class GrabPayScreen extends StatelessWidget { - const GrabPayScreen({Key? key}) : super(key: key); + const GrabPayScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -34,6 +34,8 @@ class GrabPayScreen extends StatelessWidget { // see file main.dart in this example app. // 1. on the backend create a payment intent for payment method and save the // client secret. + + final scaffoldMessenger = ScaffoldMessenger.of(context); final result = await _createPaymentIntent(); final clientSecret = await result['clientSecret']; @@ -61,23 +63,24 @@ class GrabPayScreen extends StatelessWidget { ), ), ); - - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), - ), - ); + if (context.mounted) { + scaffoldMessenger.showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } } on Exception catch (e) { - if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + if (e is StripeException && context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( content: Text('Error from Stripe: ${e.error.localizedMessage}'), ), ); - } else { - ScaffoldMessenger.of(context).showSnackBar( + } else if (context.mounted) { + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/regional_payment_methods/revolutpay_screen.dart b/example/lib/screens/regional_payment_methods/revolutpay_screen.dart index 09ee1eb8a..ec73fe02a 100644 --- a/example/lib/screens/regional_payment_methods/revolutpay_screen.dart +++ b/example/lib/screens/regional_payment_methods/revolutpay_screen.dart @@ -9,7 +9,7 @@ import 'package:stripe_example/widgets/loading_button.dart'; import '../../config.dart'; class RevolutPayScreen extends StatelessWidget { - const RevolutPayScreen({Key? key}) : super(key: key); + const RevolutPayScreen({super.key}); Future> _createPaymentIntent() async { final url = Uri.parse('$kApiUrl/create-payment-intent'); @@ -29,6 +29,9 @@ class RevolutPayScreen extends StatelessWidget { } Future _pay(BuildContext context) async { + // Store scaffold messenger reference + final scaffoldMessenger = ScaffoldMessenger.of(context); + // Precondition: //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK // see file main.dart in this example app. @@ -46,24 +49,25 @@ class RevolutPayScreen extends StatelessWidget { ), ); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Payment succesfully completed'), + if (!context.mounted) return; + scaffoldMessenger.showSnackBar( + const SnackBar( + content: Text('Payment successfully completed'), ), ); - } on Exception catch (e, s) { - throw e; + } on Exception catch (e) { + if (!context.mounted) return; if (e is StripeException) { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( content: Text( 'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'), ), ); } else { - ScaffoldMessenger.of(context).showSnackBar( + scaffoldMessenger.showSnackBar( SnackBar( - content: Text('Unforeseen error: ${e}'), + content: Text('Unforeseen error: $e'), ), ); } diff --git a/example/lib/screens/setup_future_payments/add_payment_method_screen_loader_mobile.dart b/example/lib/screens/setup_future_payments/add_payment_method_screen_loader_mobile.dart index e77f90741..1c903a959 100644 --- a/example/lib/screens/setup_future_payments/add_payment_method_screen_loader_mobile.dart +++ b/example/lib/screens/setup_future_payments/add_payment_method_screen_loader_mobile.dart @@ -1,4 +1,3 @@ -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_stripe/flutter_stripe.dart' as stripe; import 'package:flutter_stripe/flutter_stripe.dart';