flutter-stripe / flutter_stripe

Flutter SDK for Stripe.
https://pub.dev/packages/flutter_stripe
957 stars 528 forks source link

initialization error on web, throw "DartError: Unsupported operation: Platform._operatingSystem" #1952

Open dereking opened 1 month ago

dereking commented 1 month ago

Describe the bug

On web , this initialization thrown excception: initialization error on web, throw "DartError: Unsupported operation: Platform._operatingSystem"

 Stripe.publishableKey = stripePublishableKey;  

To Reproduce Steps to reproduce the behavior:

  1. pub add flutter_stripe for my project
  2. add init stripe code in main.dart
 Stripe.publishableKey = stripePublishableKey;   //error here: packages/xxxxxx/main.dart 38:10 
  Stripe.merchantIdentifier = 'com.xxxxxxxx.app';
  Stripe.urlScheme = 'flutterstripe';
  await Stripe.instance.applySettings();
  1. flutter run -d chrome
  2. Observe a failure with exception (including the part of the stack trace, belonging to this package) … An exception thrown out when page loaded:
    DartError: Unsupported operation: Platform._operatingSystem
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3       throw_
    dart-sdk/lib/_internal/js_dev_runtime/patch/io_patch.dart 244:5                   _operatingSystem
    dart-sdk/lib/io/platform_impl.dart 56:40                                          get operatingSystem
    dart-sdk/lib/io/platform.dart 83:44                                               get operatingSystem
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 1071:8  get
    dart-sdk/lib/io/platform.dart 161:46                                              get isIOS
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 1071:8  get
    packages/stripe_platform_interface/src/method_channel_stripe.dart 672:33          create
    packages/stripe_platform_interface/src/stripe_platform_interface.dart 12:72       get _instance
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 1071:8  get
    packages/stripe_platform_interface/src/stripe_platform_interface.dart 17:41       get instance
    packages/flutter_stripe/src/stripe.dart 694:35                                    get _platform
    packages/flutter_stripe/src/stripe.dart 701:20                                    markNeedsSettings
    packages/flutter_stripe/src/stripe.dart 25:5                                      set publishableKey
    packages/xxxxxx/main.dart 38:10                                                   initStripe
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                _async
    packages/xxxxxx/main.dart 35:24                                                   initStripe
    packages/xxxxxx/main.dart 50:3                                                    main$
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5                _async
    packages/xxxxxx/main.dart 45:18                                                   main$
    web_entrypoint.dart 24:31                                                         <fn>
    lib/ui_web/ui_web/initialization.dart 41:9                                        <fn>
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50                <fn>
    dart-sdk/lib/async/zone.dart 1661:54                                              runUnary
    dart-sdk/lib/async/future_impl.dart 163:18                                        handleValue
    dart-sdk/lib/async/future_impl.dart 847:44                                        handleValueCallback
    dart-sdk/lib/async/future_impl.dart 876:13                                        _propagateToListeners
    dart-sdk/lib/async/future_impl.dart 652:5                                         [_completeWithValue]
    dart-sdk/lib/async/future_impl.dart 722:7                                         callback
    dart-sdk/lib/async/schedule_microtask.dart 40:11                                  _microtaskLoop
    dart-sdk/lib/async/schedule_microtask.dart 49:5                                   _startMicrotaskLoop
    dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7                <fn>

Expected behavior Console displays the error in vscode terminal.

Smartphone / tablet

remonh87 commented 1 month ago

did you add flutter_stripe_web to you dependencies? I cannot reproduce it on our example app which has identical code

dereking commented 1 month ago

Yes, I added flutter_stripe_web to my dependencies yet.

dereking commented 1 month ago

It looks like the issue is provoked by web hot reload after a added flutter_stripe_web . I restart flutter run -d chrome, It works. But i think I should test more on iOS and Android..

dereking commented 1 month ago

Latest found:

I found that flutter-stripe depends on stripe_platform_interface: ^11.1.1

But stripe_platform_interface dosn't support web... only support Android\iOS\Linux\macOS\Windows.

stripe_err

The source code caused error is : stripe_platform_interface-11.1.1/lib/src/method_channel_stripe.dart 672:33 create

class MethodChannelStripeFactory {
  const MethodChannelStripeFactory();

  StripePlatform create() => MethodChannelStripe(
        methodChannel: const MethodChannel(
          'flutter.stripe/payments',
          JSONMethodCodec(),
        ),
        platformIsIos: Platform.isIOS,   <--------cause error: DartError: Unsupported operation: Platform._operatingSystem
        platformIsAndroid: Platform.isAndroid,
      );
}

the problem is "Platform". We need to add web support in this method:

StripePlatform create() => MethodChannelStripe(
        methodChannel: const MethodChannel(
          'flutter.stripe/payments',
          JSONMethodCodec(),
        ),
        platformIsIos: !kIsWeb && Platform.isIOS,    
        platformIsAndroid:  !kIsWeb && Platform.isAndroid,
      );

Hope can help.