ソースを参照

flutter与原生通信基础桥接封装。

PC\19500 2 週間 前
コミット
bbc4867173
1 ファイル変更38 行追加0 行削除
  1. 38 0
      lib/base/native_bridge.dart

+ 38 - 0
lib/base/native_bridge.dart

@@ -0,0 +1,38 @@
+import 'package:flutter/services.dart';
+
+class NativeBridge {
+  static const MethodChannel _channel = MethodChannel('com.sinoMedCloud.bridge');
+
+  // 通用invoke
+  static Future<T?> invoke<T>(
+    String method, {
+    Map<String, dynamic>? params,
+  }) async {
+    try {
+      final result = await _channel.invokeMethod<T>(
+          method,
+          params
+      );
+      return result;
+    } on PlatformException catch (e) {
+      throw NativeBridgeException(
+        code: e.code,
+        message: e.message ?? 'Unknown error',
+      );
+    }
+  }
+}
+
+class NativeBridgeException implements Exception {
+  final String code;
+  final String message;
+
+  NativeBridgeException({
+    required this.code,
+    required this.message,
+  });
+  
+  @override
+  String toString() => '[$code] $message';
+
+}