3종류에 대한 기능을 간략히 볼 수 있는 예시자료
Working with native elements in Flutter: Platform Channel vs Pigeon vs Foreign Function Interface (FFI) | Codemagic Blog
Method Channel이란?
Writing custom platform-specific code
- 앱의 플러터 영역에서 플랫폼 채널(Platform Channel)을 통해 non-Dart 영역인 각 플랫폼의 호스트로 메시지를 보낸다.
- 호스트는 플랫폼 채널을 통해 메시지를 수신하고 해당 메시지에 맞게 네이티브 언어로 작성된 플랫폼 특화 API들을 호출한 다음 클라이언트 영역인 플러터에 결과를 돌려준다.
플러터에서 사용할 수 있는 메서드 채널 언어는 다음과 같다.
- Kotlin or Java on Android
- Swift or Objective-C on iOS
- C++ on Windows
- Objective-C on macOS
- C on Linux
- 웹은 메서드 채널 대신 dart:html 라이브러리를 사용하면 된다.
일반적으로 메서드 채널은 다음과 같이 채널을 개통하면서 시작한다.
static const _methodChannel =
MethodChannel("com.example.text_editors/action");
//구현부
const MethodChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger ])
: _binaryMessenger = binaryMessenger;
메서드 채널 객체는 3가지 파라미터를 가진다.
- name
- 채널을 식별할 때 사용한다. 각 채널마다 고유한 값이여야 하며 호스트 플랫폼과 반드시 일치해야 한다.
- codec
- 플랫폼마다 사용할 코덱, 이른바 데이터 타입을 정의해준다. 옵셔널 파라미터이며 기본적으로 StandardMethodCodec 객체를 사용한다. 참고
- BinaryMessenger
- byte 데이터를 주고 받을 바이너리메신저를 넣어준다. 기본값은 BinaryMessenger를 사용한다.