Block constructor
Block(
- Function function
Creating a Block from a Function.
NOTE: The arguments of function
should be wrapper class which can
represent native type, such as unsigned_int or custom wrapper class with
the same name.
When you create a Block, you release it using Block_release
after use.
Implementation
factory Block(Function function) {
List<String> dartTypes = dartTypeStringForFunction(function);
bool shouldReturnAsync = dartTypes.first.startsWith('Future');
// block receives results from dart function asynchronously by appending a callback function to arguments.
if (shouldReturnAsync) {
dartTypes.add('Function');
}
List<String> nativeTypes = nativeTypeStringForDartTypes(dartTypes);
Pointer<Utf8> typeStringPtr = nativeTypes.join(', ').toNativeUtf8();
Pointer<Void> blockPtr = blockCreate(
typeStringPtr, _callbackPtr, shouldReturnAsync ? 1 : 0, nativePort);
assert(blockPtr != nullptr);
if (blockPtr == nullptr) {
return nilBlock;
}
int sequence = _blockSequence(blockPtr);
Block result = Block.fromPointer(blockPtr, function: function);
calloc.free(typeStringPtr);
result.types = dartTypes;
result.shouldReturnAsync = shouldReturnAsync;
result.typeEncodingsPtrPtr = _blockTypeEncodings(blockPtr);
result.sequence = sequence;
if (blockForSequence[sequence] != null) {
throw 'Already exists a block on sequence $sequence';
}
blockForSequence[sequence] = result;
return result;
}