How Get value back using Pigeon FlutterApi from native iOS/Android?

125 views Asked by At

Using Pigeon package to communicate between Flutter and native iOS/Android.

I want to call a flutter function from native and get a value back.

The function on flutter is defined like this:

@FlutterApi()
abstract class FlutterApi {
    Bool askConfirmation();
}

In swift i'm using this :

let flutterApi: FlutterApi?

func callAskConfirmation() -> Bool {
    var result = false
    flutterApi?.askConfirmation(completion: { value in
        result = value
    })
    return result
}

How I can wait the completion of askConfirmation to return the result ? Is it possible ?

I have test Semaphore to wait the result, it dosn't work (May be bad using the Dispatcher). Code is blocked on the semaphore.wait :

let flutterApi: FlutterApi?

func callAskConfirmation() -> Bool {
        let dispatchQueueProcess = DispatchQueue.global(qos: .userInitiated)
        var result = false
        let semaphore = DispatchSemaphore(value: 0)
        dispatchQueueProcess.async {
            self.flutterApi?.askConfirmation(completion: { test in
                result = test
                semaphore.signal()
            })
        }
        semaphore.wait()
        return result
}
0

There are 0 answers