CodeBlanks Logo Icon CodeBlanks

setMethodCallHandler

Receive native calls with setMethodCallHandler.

What is setMethodCallHandler?

setMethodCallHandler listens to method calls coming from the native side to Dart. When the native side calls a method, the registered callback runs. This allows Dart to receive communication initiated by the native side.

channel.setMethodCallHandler((call) async {
  if (call.method == 'notify') {
    print('Notification received');
  }
});

The MethodCall object

The handler callback takes a MethodCall object. You read the method name with call.method and the sent arguments with call.arguments. With this information you process the request from the native side.

channel.setMethodCallHandler((call) async {
  print(call.method);
  print(call.arguments);
});

Practice Register a handler and trigger it

Create a MethodChannel, register a handler with setMethodCallHandler and print call.method inside the handler. Then trigger the handler with channel.simulateCall('notify'). Wait 10 milliseconds.

Starter code

main.dart
class MethodCall {
  final String method;
  MethodCall(this.method);
}

class MethodChannel {
  final String name;
  void Function(MethodCall)? _handler;
  MethodChannel(this.name);
  void setMethodCallHandler(void Function(MethodCall) handler) {
    _handler = handler;
  }
  void simulateCall(String method) {
    if (_handler != null) _handler!(MethodCall(method));
  }
}

void main() async {
  // MethodChannel oluştur
  // handler kaydet, call.method yazdır
  // simulateCall ile tetikle
}

Sample solution

main.dart
class MethodCall {
  final String method;
  MethodCall(this.method);
}

class MethodChannel {
  final String name;
  void Function(MethodCall)? _handler;
  MethodChannel(this.name);
  void setMethodCallHandler(void Function(MethodCall) handler) {
    _handler = handler;
  }
  void simulateCall(String method) {
    if (_handler != null) _handler!(MethodCall(method));
  }
}

void main() async {
  const channel = MethodChannel('samples.flutter.dev/battery');
  channel.setMethodCallHandler((call) {
    print(call.method);
  });
  channel.simulateCall('notify');
  await Future.delayed(Duration(milliseconds: 10));
}

Expected output

notify

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 When does setMethodCallHandler fire?

  • When Dart calls a native method
  • When the native side calls a method on Dart Correct answer
  • It never fires, it is for definition only
  • Only when the app starts

Explanation: setMethodCallHandler fires when the native side calls a method on Dart; Dart calling native is done with invokeMethod.

2 Register a handler

Question
channel.____((call) async {
  print(call.method);
});
Answer
channel.setMethodCallHandler((call) async {
  print(call.method);
});

Explanation: To listen to calls from the native side you call the setMethodCallHandler method and provide a callback.

3 setMethodCallHandler parts

Term Meaning
setMethodCallHandler Registers a handler
MethodCall Call object
call.method Method name
callback Handler function

All lessons in Advanced