zhouxinle
zhouxinle

Reputation: 469

Flutter Plugin - Value Of type "xxx" has no member "xxx"

I write a flutter iOS plugin which using Google Machine learning framework 'ML Kit'. The iOS plugin language is swift. Plugin has 3 files. MLKitPlugin.h, MLKitPlugin.m, SwiftMLKitPlugin.swift, these 3 file are generated automaticly when I am creating this plugin.
This is code of iOS plugin .podspec file:

Pod::Spec.new do |s|
  s.name             = 'image_origin_enhancer'
  s.version          = '0.0.1'
  s.summary          = 'image origin enhancer'
  s.description      = <<-DESC
image origin enhancer
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'Your Company' => '[email protected]' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.static_framework = true
  s.dependency 'Flutter'
  # here import Google ML Kit framework
  s.dependency 'GoogleMLKit/PoseDetection', '2.6.0'

  # Flutter.framework does not contain a i386 slice.
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
  s.swift_version = '5.0'
end

Code in MLKitPlugin.m is very simple:

#import "MLKitPlugin.h"
#if __has_include(<ml_kit/ml_kit-Swift.h>)
#import <ml_kit/ml_kit-Swift.h>
#else
// Support project import fallback if the generated compatibility header
// is not copied when this plugin is created as a library.
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
#import "ml_kit-Swift.h"
#endif

@implementation MLKitPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  [SwiftMLKitPlugin registerWithRegistrar:registrar];
}

Code in SwiftMLKitPlugin.swift

import Flutter
import UIKit
import MLImage
import MLKitPoseDetection
import MLKitPoseDetectionCommon

public class SwiftMLKitPlugin: NSObject, FlutterPlugin {
     var poseDetector: PoseDetector? = nil  // define PoseDetector from MLKit framework

  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "XXX", binaryMessenger: registrar.messenger())
    let instance = SwiftMLKitPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }
    
    override init(){
        super.init();
        let options = PoseDetectorOptions()
        options.detectorMode = .singleImage
        self.poseDetector = PoseDetector.poseDetector(options: options)
    }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      if "detectPose" == call.method {
          if let poseDetector = self.poseDetector {
            poseDetector.process(MLImage(image:UIImage(name:""))) // this is the error code
 { poses, error in
              guard error == nil, let poses = poses, !poses.isEmpty else {
                let errorString = error?.localizedDescription ?? Constants.detectionNoResultsMessage
                return
              }
            }
          }
          
          result("success")
      }
  }
}

poseDetector.process(MLImage(image:UIImage(name:""))) is the error code. The error message is "value of type 'PoseDetector' has no member 'process'". We can see 'process' method has been defined in 'PoseDetector' class:

NS_SWIFT_NAME(PoseDetector)
@interface MLKPoseDetector : NSObject
...

- (void)processImage:(id<MLKCompatibleImage>)image
          completion:(MLKPoseDetectionCallback)completion NS_SWIFT_NAME(process(_:completion:));

@end

The problem is very strange, if I Write these codes in MLKitPlugin.m using Objective-C, it is built successfully. Also if I write a swift origin project(not flutter plugin), it is also built successfully. So is this problem related with Flutter? is this a project problem. Do I missed something?
Addition, 'Podfile' in main project:

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
  end
end

Upvotes: 0

Views: 1763

Answers (2)

Enyor pi&#241;a
Enyor pi&#241;a

Reputation: 1

It is an import error in the package, you can fix it locally.

Upvotes: 0

zhouxinle
zhouxinle

Reputation: 469

I resolved this problem by

import MLKitVision 

Full import in SwiftMLKitPlugin.swift like this:

import MLImage
import MLKitPoseDetection
import MLKitPoseDetectionCommon
import MLKitVision

Upvotes: 1

Related Questions