import 'dart:ffi'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'classify_photo_platform_interface.dart'; /// An implementation of [ClassifyPhotoPlatform] that uses method channels. class MethodChannelClassifyPhoto extends ClassifyPhotoPlatform { /// The method channel used to interact with the native platform. @visibleForTesting final methodChannel = const MethodChannel('classify_photo'); @override Future getPlatformVersion() async { final version = await methodChannel.invokeMethod('getPlatformVersion'); return version; } @override Future checkTrialEligibility(String appleId) async { try { // 调用原生方法并处理可能的空值 final result = await methodChannel.invokeMethod('checkTrialEligibility', {'appleId': appleId}, ); return result ?? false; // 如果结果为 null,返回 false } on PlatformException catch (e) { print('检查试用资格失败: ${e.message}'); return false; // 发生错误时返回 false } catch (e) { print('未知错误: $e'); return false; // 其他错误情况返回 false } } @override Future>?> getPhoto() async { try { print('Flutter: 调用 getPhoto 方法'); final List? result = await methodChannel.invokeMethod>( 'getPhoto', ); print('Flutter: 收到原生返回结果: $result'); if (result == null) { print('Flutter: 结果为空'); return null; } return result.map((group) => Map.from(group)).toList(); } on PlatformException catch (e) { print('Flutter: Platform Exception: ${e.message}'); rethrow; } catch (e) { print('Flutter: Error: $e'); rethrow; } } // 实现获取存储信息的方法 @override Future> getStorageInfo() async { try { final result = await methodChannel.invokeMethod>('getStorageInfo'); if (result == null) { return {}; } // 转换返回的数据类型 return result.map((key, value) => MapEntry( key.toString(), (value as num).toInt(), )); } on PlatformException catch (e) { debugPrint('获取存储信息失败: ${e.message}'); return {}; } } @override Future> getPhotoExif(String filePath) async { try { final result = await methodChannel.invokeMethod>( 'getExifInfo', {'filePath': filePath}, ); // 转换结果为 Map return _convertMap(result ?? {}); } catch (e) { print('获取照片 EXIF 信息失败: $e'); return {}; } } /// 转换 Map 类型 Map _convertMap(Map map) { return map.map((key, value) { if (value is Map) { return MapEntry(key.toString(), _convertMap(value)); } return MapEntry(key.toString(), value); }); } @override Future finishTransaction() async { print('Flutter: 清除自动订阅记录'); await methodChannel.invokeMethod>('finishTran'); } }