classify_photo_method_channel.dart 1.8 KB

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