Maksimka101 / isolate-bloc

A Flutter package that helps implement the BLoC pattern when blocs run in their own Isolate.
MIT License
21 stars 9 forks source link

Can't register specific method channel (spotify_sdk) #27

Closed Christer-Muntean closed 2 years ago

Christer-Muntean commented 2 years ago

Hi πŸ‘‹ This is not an issue per say, but I'm struggling to add spotify_sdk plugin method channel.

This is my code

Future<void> main() async {
 await initialize(
    isolatedFunc,
    methodChannelSetup: MethodChannelSetup(
      methodChannelNames: [
        'plugins.flutter.io/url_launcher',
        'plugins.flutter.io/firebase_firestore',
        'spotify_sdk',
        'player_context_subscription',
        'player_state_subscription',
        'user_status_subscription',
        'capabilities_subscription',
        'connection_status_subscription',
      ],
    ),
  );

Where as I'm prompted with the following error:

flutter: ══║ EXCEPTION CAUGHT BY SERVICES LIBRARY β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
flutter: The following _Exception was thrown while activating platform stream on channel
flutter: connection_status_subscription:
flutter: Exception: Platform messages can only be sent from the main isolate
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      PlatformDispatcher.sendPlatformMessage (dart:ui/platform_dispatcher.dart:510:7)
flutter: #1      _DefaultBinaryMessenger.send (package:flutter/src/services/binding.dart:354:36)
flutter: #2      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:52)
flutter: #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:350:12)
flutter: #4      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:516:29)
flutter: #5      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:502:64)
flutter: #15     CustomPlaylistCubit2.connect (package:uqueue/pages/playlist_page_2/custom_playlist_cubit2.dart:29:44)
flutter: #16     new CustomPlaylistCubit2 (package:uqueue/pages/playlist_page_2/custom_playlist_cubit2.dart:15:5)
flutter: #17     isolatedFunc.<anonymous closure> (package:uqueue/main.dart:97:68)
flutter: #18     IsolateManager.registerBloc (package:isolate_bloc/src/common/isolate/manager/isolate_manager.dart:77:27)
flutter: #19     register (package:isolate_bloc/src/common/isolated_api_wrappers.dart:33:20)
flutter: #20     isolatedFunc (package:uqueue/main.dart:97:3)
flutter: #21     IsolateManager.initialize (package:isolate_bloc/src/common/isolate/manager/isolate_manager.dart:59:29)
flutter: #22     IsolateInitializer._isolatedBlocRunner (package:isolate_bloc/src/common/isolate/initializer/isolate_initializer.dart:49:26)
flutter: #23     IOIsolateFactory._runInIsolate (package:isolate_bloc/src/common/isolate/isolate_factory/isolate/io_isolate_factory.dart:120:21)
flutter: #24     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:17)
flutter: (elided 10 frames from class _RawReceivePortImpl and dart:async)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: QueueState.active_queue_not_exists

Does isolate_bloc not support EventChannels ? Or am I doing something wrong ?

Best regards, love your plugin! Please add a paypal.me or buymeacoffee for donations 😊

Maksimka101 commented 2 years ago

Hi @Christer-Muntean,

Your code is correct so it looks like either I broke method channels or a new flutter version broke them :)

I'll figure out it and make a hotfix in the near future.

Maksimka101 commented 2 years ago

I have a veeery bad news. Seems like there is no way to use method channels in the isolate since flutter team removed MethodChannel.setMockMethodCallHandler function. It should be noted that flutter_isolate package lets us to do so however it restricts us in sending non primitive objects so unfortunately it can't be used.

I'll try to find the way to solve this problem but it seems unlikely.

Update: seems like I found another way to override method channels and it is very promising

Christer-Muntean commented 2 years ago

Ah, I see πŸ™‚ Well I'll try and use flutter_isolate then. πŸ‘

Maksimka101 commented 2 years ago

Fixed with #29