classify_photo_method_channel.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'dart:ffi';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/services.dart';
  4. import 'classify_photo_platform_interface.dart';
  5. /// An implementation of [ClassifyPhotoPlatform] that uses method channels.
  6. class MethodChannelClassifyPhoto extends ClassifyPhotoPlatform {
  7. /// The method channel used to interact with the native platform.
  8. @visibleForTesting
  9. final methodChannel = const MethodChannel('classify_photo');
  10. @override
  11. Future<String?> getPlatformVersion() async {
  12. final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
  13. return version;
  14. }
  15. @override
  16. Future<bool> checkTrialEligibility() async {
  17. try {
  18. // 调用原生方法并处理可能的空值
  19. final result = await methodChannel.invokeMethod<bool>('checkTrialEligibility');
  20. return result ?? false; // 如果结果为 null,返回 false
  21. } on PlatformException catch (e) {
  22. print('检查试用资格失败: ${e.message}');
  23. return false; // 发生错误时返回 false
  24. } catch (e) {
  25. print('未知错误: $e');
  26. return false; // 其他错误情况返回 false
  27. }
  28. }
  29. @override
  30. Future<List<Map<String, dynamic>>?> getPhoto() async {
  31. try {
  32. print('Flutter: 调用 getPhoto 方法');
  33. final List<dynamic>? result = await methodChannel.invokeMethod<List<dynamic>>(
  34. 'getPhoto',
  35. );
  36. print('Flutter: 收到原生返回结果: $result');
  37. if (result == null) {
  38. print('Flutter: 结果为空');
  39. return null;
  40. }
  41. return result.map((group) => Map<String, dynamic>.from(group)).toList();
  42. } on PlatformException catch (e) {
  43. print('Flutter: Platform Exception: ${e.message}');
  44. rethrow;
  45. } catch (e) {
  46. print('Flutter: Error: $e');
  47. rethrow;
  48. }
  49. }
  50. // 实现获取存储信息的方法
  51. @override
  52. Future<Map<String, int>> getStorageInfo() async {
  53. try {
  54. final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>('getStorageInfo');
  55. if (result == null) {
  56. return {};
  57. }
  58. // 转换返回的数据类型
  59. return result.map((key, value) => MapEntry(
  60. key.toString(),
  61. (value as num).toInt(),
  62. ));
  63. } on PlatformException catch (e) {
  64. debugPrint('获取存储信息失败: ${e.message}');
  65. return {};
  66. }
  67. }
  68. }