native_bridge.dart 793 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'package:flutter/services.dart';
  2. class NativeBridge {
  3. static const MethodChannel _channel = MethodChannel('com.sinoMedCloud.bridge');
  4. // 通用invoke
  5. static Future<T?> invoke<T>(
  6. String method, {
  7. Map<String, dynamic>? params,
  8. }) async {
  9. try {
  10. final result = await _channel.invokeMethod<T>(
  11. method,
  12. params
  13. );
  14. return result;
  15. } on PlatformException catch (e) {
  16. throw NativeBridgeException(
  17. code: e.code,
  18. message: e.message ?? 'Unknown error',
  19. );
  20. }
  21. }
  22. }
  23. class NativeBridgeException implements Exception {
  24. final String code;
  25. final String message;
  26. NativeBridgeException({
  27. required this.code,
  28. required this.message,
  29. });
  30. @override
  31. String toString() => '[$code] $message';
  32. }