ShoutSocial / share_handler

A plugin to facilitate receiving and handling share intents
43 stars 45 forks source link

Works on iOS not on Android #3

Closed sukhcha-in closed 2 years ago

sukhcha-in commented 2 years ago

I'm getting this exception on Android specifically.

E/flutter (32405): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'SharedMedia' is not a subtype of type 'Map<Object?, Object?>' in type cast
E/flutter (32405): #0      SharedMedia.decode (package:share_handler_platform_interface/src/data/messages.dart:74)
E/flutter (32405): #1      ShareHandlerApi.getInitialSharedMedia (package:share_handler_platform_interface/src/data/messages.dart:155)
E/flutter (32405): <asynchronous suspension>
E/flutter (32405): #2      MethodChannelShareHandler.getInitialSharedMedia (package:share_handler_platform_interface/src/method_channel/method_channel_share_handler.dart:25)
E/flutter (32405): <asynchronous suspension>
E/flutter (32405): #3      IntentController.initPlatformState (package:app/global/controllers/intent_controller.dart:35)
E/flutter (32405): <asynchronous suspension>
E/flutter (32405):

Handler:

void initPlatformState() async {
    //deeplinks
    final appLinks = AppLinks();
    //shared links
    final sharedLinks = ShareHandlerPlatform.instance;

    //Cold Boot shared links
    SharedMedia? media = await sharedLinks.getInitialSharedMedia();
    if (media != null && media.content != null) {
      handler(media.content.toString());
    }

    //Cold Boot applinks
    final initialAppLink = await appLinks.getInitialAppLink();
    if (initialAppLink != null) {
      handler(initialAppLink.toString());
    }

    //Shared Links Listener
    sharedLinks.sharedMediaStream.listen((SharedMedia m) {
      handler(m.content.toString());
    });

    //Applinks Listener
    appLinks.uriLinkStream.listen((uri) {
      handler(uri.toString());
    });
  }
sukhcha-in commented 2 years ago

It was having some conflicts with AppLinks package, so wrapped both initial links in try catch.

try {
      //Cold Boot shared links
      SharedMedia? media = await sharedLinks.getInitialSharedMedia();
      if (media != null && media.content != null) {
        print('Cold Boot shared links');
        handler(media.content.toString());
      }
    } catch (e) {
        log(e.toString());
    }

    try {
      //Cold Boot applinks
      final initialAppLink = await appLinks.getInitialAppLink();
      if (initialAppLink != null) {
        print('Cold Boot applinks');
        handler(initialAppLink.toString());
      }
    } catch (e) {
        log(e.toString());
    }
daviddomkar commented 2 years ago

Hi. I encountered the same problem while using this package in an existing app but I am not using AppLinks. I think it is also a conflict with another package because it works just fine on a newly created app. Any idea how to debug this?