How to test method channel that call from native to Flutter?


I’m developing a native plugin and trying to do unit tests.
All unit tests will be done in Dart (No native code).

Flutter has a test example of how you can test call method channel from Dart to native using setMockMethodCallHandler.

The problem is I’ve not found the way to test method channel that calls from native to Dart that uses setMethodCallHandler to handle a call from native.

Here is an example

// main.dart

class Plugin {

  static MethodChannel _channel = const MethodChannel('plugin');
  Plugin() {
    _channel.setMethodCallHandler((call) async {
      print("called from native: ${call.method}");

// tests/main_test.dart

void main() {
  const MethodChannel _channel = MethodChannel('core.super_router');
  Plugin plugin;

  setUp(() async {
    plugin = Plugin();

  test("call from native", () async {
    // This call can't reach the handler in the Plugin
    // And there is no method like mockInvokeMethod


Snippet below based on
approach, works perfect for me.

test("Listening device", () async {

        MethodCall('methodName', '{"result":true}')),(ByteData data){});


Answered By – Álvaro Menezes

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *