ShoutSocial / share_handler

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

getInitialSharedMedia don't works #73

Closed Reymai closed 3 weeks ago

Reymai commented 1 year ago

Hi! In my code didn't work getInitialSharedMedia function. I can get shared media by the sharedMediaStream, but initial shared media I can't get.

share handler:

import 'dart:async';
import 'dart:developer';

import 'package:share_handler/share_handler.dart';

abstract class IShareToHandler {
  Future<void> initPlatformState();
  SharedMedia? get sharedMedia;
  Stream<SharedMedia> get sharedMediaStream;
}

class ShareToHandler implements IShareToHandler {
  SharedMedia? _sharedMedia;
  final StreamController<SharedMedia> _sharedMediaStreamController =
      StreamController();

  ShareToHandler() {
    initPlatformState();
  }

  @override
  Future<void> initPlatformState() async {
    final handler = ShareHandlerPlatform.instance;
    _sharedMedia = await handler.getInitialSharedMedia();

    handler.sharedMediaStream.listen((SharedMedia media) {
      log('Shared media: ${media.toString()}');
      _sharedMedia = media;
      _sharedMediaStreamController.add(media);
    });
  }

  @override
  SharedMedia? get sharedMedia => _sharedMedia;
  @override
  Stream<SharedMedia> get sharedMediaStream =>
      _sharedMediaStreamController.stream.asBroadcastStream();
}

main:

final getIt = GetIt.I;

Future<void> singletonRegister() async {
  getIt.registerSingleton<IShareToHandler>(ShareToHandler());
  // ...
}

Future<void> bootstrap() async {
  WidgetsFlutterBinding.ensureInitialized();
  // ...
  singletonRegister();
  getIt.get<IShareToHandler>().initPlatformState();
}

void main() async {
  await bootstrap();
  runApp(
    MainApp(),
  );
}

flutter doctor:

[✓] Flutter (Channel stable, 3.13.6, on macOS 14.0 23A344 darwin-arm64, locale en-LV)
    • Flutter version 3.13.6 on channel stable at /Users/raimondsmakars/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ead455963c (4 weeks ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/raimondsmakars/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/raimondsmakars/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.13.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Users/raimondsmakars/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.4)
    • IntelliJ at /Users/raimondsmakars/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 232.10072.19

[✓] VS Code (version 1.83.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.0

[✓] VS Code (version 1.84.0-insider)
    • VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
    • Flutter extension version 3.74.0

[✓] Connected device (3 available)
    • Pixel 6 (mobile) • 26211FDF60055U • android-arm64  • Android 14 (API 34)
    • macOS (desktop)  • macos          • darwin-arm64   • macOS 14.0 23A344 darwin-arm64
    • Chrome (web)     • chrome         • web-javascript • Google Chrome 118.0.5993.117

[✓] Network resources
    • All expected network resources are available.

• No issues found!
tmratwork commented 1 year ago

I am running the example code, cannot get app working in iOS. it builds, but the event handler does not get called. share_handler: ^0.0.19

Reymai commented 1 year ago

I am running the example code, cannot get app working in iOS. it builds, but the event handler does not get called. share_handler: ^0.0.19

You mean initPlatformState? It calls in main in bootstrap function. In initPlatformState calls getInitialSharedMedia too, but it's return null. It's return null when start application on "cold" without sharing, when start application on "cold" by sharing something to app, and, obviously, null, when share something in app, when app already is running

tmratwork commented 1 year ago

I re-installed the plugin to get more information before replying, and now it works as expected. only thing I did different was use default instead of custom group id.

JoshJuncker commented 3 weeks ago

Closing this as it seems to be an mis-configuration issue. If you still have questions, feel free to reopen with more details.