[Flutter] 플랫폼 채널

GH lee·2024년 1월 2일
0

Dart/Flutter

목록 보기
9/12
post-thumbnail

플러터 공식문서를 번역하여 정리한 글입니다.

렌더링 프로세스

목차

  1. 레이어
  2. 반응형 UI
  3. 위젯
  4. 렌더링 프로세스
  5. 플랫폼 채널

플랫폼 채널

플러터에서는 플랫폼 채널을 통해 dart 코드와
플랫폼별 코드를 연결할 수 있다.
플랫폼 채널을 생성하여 코틀린이나 스위프트 등으로 작성된 코드와
데이터를 주고 받을 수 있으며
이 때 데이터는 dart의 map, 코틀린의 hashmap, 스위프트의 dictionary 형식으로
직렬화, 역직렬화 된다.

예시 코드)

// Dart side
const channel = MethodChannel('foo');
final greeting = await channel.invokeMethod('bar', 'world') as String;
print(greeting);
// Android (Kotlin)
val channel = MethodChannel(flutterView, "foo")
channel.setMethodCallHandler { call, result ->
  when (call.method) {
    "bar" -> result.success("Hello, ${call.arguments}")
    else -> result.notImplemented()
  }
}
// iOS (Swift)
let channel = FlutterMethodChannel(name: "foo", binaryMessenger: flutterView)
channel.setMethodCallHandler {
  (call: FlutterMethodCall, result: FlutterResult) -> Void in
  switch (call.method) {
    case "bar": result("Hello, \(call.arguments as! String)")
    default: result(FlutterMethodNotImplemented)
  }
}
profile
Flutter Junior

0개의 댓글