androidflutterpigeon

Trouble with Pigeon: Unable to bridge between Dart and Kotlin in debug mode


I'm facing an issue with Pigeon in Flutter. In debug mode, my code fails to bridge between the pigeon_api.dart (file from flutter code) and pigeon_api.kt (file from native android side)

Pigeon_api.dart:

// Autogenerated from Pigeon (v10.1.4), do not edit directly.
...class NavigationApiHost {
  NavigationApiHost({BinaryMessenger? binaryMessenger})
      : _binaryMessenger = binaryMessenger;
  final BinaryMessenger? _binaryMessenger;

  static const MessageCodec<Object?> codec = StandardMessageCodec();

  Future<void> back() async {
    final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
        'dev.flutter.pigeon.cts_lecture_badgeo_flutter.NavigationApiHost.back', codec,
        binaryMessenger: _binaryMessenger);
    final List<Object?>? replyList =
        await channel.send(null) as List<Object?>?;
    if (replyList == null) {
      throw PlatformException(
        code: 'channel-error',
        message: 'Unable to establish connection on channel.',
      );
    } else if (replyList.length > 1) {
      throw PlatformException(
        code: replyList[0]! as String,
        message: replyList[1] as String?,
        details: replyList[2],
      );
    } else {
      return;
    }
  }
}

pigeon_api.kt

// Autogenerated from Pigeon (v10.1.4), do not edit directly.
...
interface NavigationApiHost {
  fun back()

  companion object {
    /** The codec used by NavigationApiHost. */
    val codec: MessageCodec<Any?> by lazy {
      StandardMessageCodec()
    }
    /** Sets up an instance of `NavigationApiHost` to handle messages through the `binaryMessenger`. */
    @Suppress("UNCHECKED_CAST")
    fun setUp(binaryMessenger: BinaryMessenger, api: NavigationApiHost?) {
      run {
        val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.cts_lecture_badgeo_flutter.NavigationApiHost.back", codec)
        if (api != null) {
          channel.setMessageHandler { _, reply ->
            var wrapped: List<Any?>
            try {
              api.back()
              wrapped = listOf<Any?>(null)
            } catch (exception: Throwable) {
              wrapped = wrapError(exception)
            }
            reply.reply(wrapped)
          }
        } else {
          channel.setMessageHandler(null)
        }
      }
    }
  }
}

I've ensured that the channel name between the two files is consistent, and I'm using the same version of Pigeon in both. I've initialized my APIs in the mainActivity of my native code.

mainActivity.kt

class MainActivity : FlutterFragmentActivity() {
 override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        NavigationApiHostImpl.initNavigationApiHost(flutterEngine, this)
 }
}````

Can anyone assist me with this?
I have no error logs ...

Thanks in advance


Solution

  • If all the pigeon files are generated successfully after running the build_runner then try this:

    //Do not change anything in auto generated files

    class MainActivity : FlutterActivity() {
    
        override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
    
        val engineExecutorMessenger = flutterEngine.dartExecutor.binaryMessenger
    
    
       Pigeon.MyApi.setup(engineExecutorMessenger, EventApi(context))
    
    //Pigeon is the generated java class from pigeon commands 
    //--java_out ./android/app/src/main/java/io/flutter/plugins/Pigeon.java
    
    //MyApi is the abstract class with @HostApi() annotation at flutter side
    
    
    }
    
    //private class
    
      private class EventApi(ctx: Context) : Pigeon.MyApi {
        val context: Context
    
     init {
            context = ctx
        }
    
    //method which are defined in MyApi abstract class will override here
    //example: 
        override fun testMethod(): Boolean {
            return true
        }
    
    }
    }