bufferking
bufferking

Reputation: 13

How to call a Dart method from Kotlin using the pigeon library in Flutter

I'm working on a Flutter app that uses the pigeon library to interface with the native Spotify SDK. I have defined a SpotifyAPICallback interface in the pigeon file, which contains the onAuthCode and onAuthFailure methods that should be implemented on the Dart side.

Here's the relevant code:

Pigeon Declaration File (pigeons/spotify_api.dart):

import 'package:pigeon/pigeon.dart';

class SpotifyAuthCodeParams {
  late String clientId;
  late String redirectUri;
  late String scope;
}

@ConfigurePigeon(
  // ... pigeon configuration
)

@FlutterApi()
abstract class SpotifyAPICallback {
  void onAuthCode(String spotifyAuthCode);
  void onAuthFailure(String error);
}

@HostApi()
abstract class SpotifyAPI {
  void openSpotifyConsentView(SpotifyAuthCodeParams params);
}

Kotlin Implementation (SpotifyAPIImpl.kt):

class SpotifyAPIImpl(val engine: FlutterEngine) : SpotifyAPI {
    private var apiCallback: SpotifyAPICallback? = null
    // ...

    override fun openSpotifyConsentView(params: SpotifyAuthCodeParams) {
        // ... open Spotify consent view
    }

    private fun authFlow(resultCode: Int, data: Intent?) {
        // ...
        when (response.type) {
            AuthorizationResponse.Type.CODE -> {
                println("Going back to flutter...")
                apiCallback!!.onAuthCode(response.code) // This is not being called on the Dart side
            }
            else -> apiCallback!!.onAuthFailure(response.error) {}
        }
    }
}

Dart Implementation (SpotifyAPICallbackImpl.dart):

class SpotifyAPICallbackImpl implements SpotifyAPICallback {
  @override
  void onAuthCode(String spotifyAuthCode) {
    print("Auth code from flutter: $spotifyAuthCode");
  }

  @override
  void onAuthFailure(String error) {
    throw UnimplementedError();
  }
}

On the Kotlin side, I'm able to receive the Spotify auth code successfully. However, when I try to call the onAuthCode method from the Kotlin side, it's not being called on the Dart side.

I've tried adding a setCallbackInstance method to the SpotifyAPI interface to pass the SpotifyAPICallbackImpl instance, but pigeon doesn't allow using @FlutterApi classes in the @HostApi interface.

Can someone please guide me on how to properly set up the communication between the Dart and Kotlin layers using the pigeon library, so that I can call the onAuthCode method from the Kotlin side and have it executed on the Dart side?

Any help or suggestions would be greatly appreciated.

Upvotes: 0

Views: 403

Answers (1)

HII
HII

Reputation: 4109

In your SpotifyAPICallbackImpl dart class, you should call setUp on the SpotifyAPICallback pigeon class, and pass to it this as first argument, also you can pass it messageChannelSuffix if you need it:

class SpotifyAPICallbackImpl implements SpotifyAPICallback {
  SpotifyAPICallbackImpl(){
    SpotifyApiCallback.setUp(
      this,
      messageChannelSuffix: "your_suffix_if_needed",
    );
  }
  
  @override
  void onAuthCode(String spotifyAuthCode) {
    print("Auth code from flutter: $spotifyAuthCode");
  }

  @override
  void onAuthFailure(String error) {
    throw UnimplementedError();
  }
}

Upvotes: 0

Related Questions