@@ -26,6 +26,12 @@ String _className(String prefix, String className) {
2626 }
2727}
2828
29+ String _callbackForType (String dartType, String objcType) {
30+ return dartType == 'void'
31+ ? 'void(^)(NSError*)'
32+ : 'void(^)($objcType *, NSError*)' ;
33+ }
34+
2935const Map <String , String > _objcTypeForDartTypeMap = < String , String > {
3036 'bool' : 'NSNumber *' ,
3137 'int' : 'NSNumber *' ,
@@ -101,10 +107,13 @@ void generateObjcHeader(ObjcOptions options, Root root, StringSink sink) {
101107 if (api.location == ApiLocation .host) {
102108 indent.writeln ('@protocol $apiName ' );
103109 for (Method func in api.methods) {
104- final String returnType = _className (options.prefix, func.returnType);
110+ final String returnTypeName =
111+ _className (options.prefix, func.returnType);
105112 final String argType = _className (options.prefix, func.argType);
113+ final String returnType =
114+ func.returnType == 'void' ? 'void' : '$returnTypeName *' ;
106115 indent.writeln (
107- '-($returnType * )${func .name }:($argType *)input error:(FlutterError **)error;' );
116+ '-($returnType )${func .name }:($argType *)input error:(FlutterError **)error;' );
108117 }
109118 indent.writeln ('@end' );
110119 indent.writeln ('' );
@@ -118,8 +127,9 @@ void generateObjcHeader(ObjcOptions options, Root root, StringSink sink) {
118127 for (Method func in api.methods) {
119128 final String returnType = _className (options.prefix, func.returnType);
120129 final String argType = _className (options.prefix, func.argType);
130+ final String callbackType = _callbackForType (func.returnType, returnType);
121131 indent.writeln (
122- '- (void)${func .name }:($argType *)input completion:(void(^)($ returnType *, NSError*) )completion;' );
132+ '- (void)${func .name }:($argType *)input completion:($ callbackType )completion;' );
123133 }
124134 indent.writeln ('@end' );
125135 }
@@ -176,9 +186,14 @@ void _writeHostApiSource(Indent indent, ObjcOptions options, Api api) {
176186 _className (options.prefix, func.returnType);
177187 indent.writeln ('$argType *input = [$argType fromMap:message];' );
178188 indent.writeln ('FlutterError *error;' );
179- indent.writeln (
180- '$returnType *output = [api ${func .name }:input error:&error];' );
181- indent.writeln ('callback(wrapResult([output toMap], error));' );
189+ final String call = '[api ${func .name }:input error:&error]' ;
190+ if (func.returnType == 'void' ) {
191+ indent.writeln ('$call ;' );
192+ indent.writeln ('callback(wrapResult(nil, error));' );
193+ } else {
194+ indent.writeln ('$returnType *output = $call ;' );
195+ indent.writeln ('callback(wrapResult([output toMap], error));' );
196+ }
182197 });
183198 });
184199 indent.write ('else ' );
@@ -213,8 +228,10 @@ void _writeFlutterApiSource(Indent indent, ObjcOptions options, Api api) {
213228 for (Method func in api.methods) {
214229 final String returnType = _className (options.prefix, func.returnType);
215230 final String argType = _className (options.prefix, func.argType);
231+ final String callbackType = _callbackForType (func.returnType, returnType);
232+
216233 indent.write (
217- '- (void)${func .name }:($argType *)input completion:(void(^)($ returnType *, NSError*) )completion ' );
234+ '- (void)${func .name }:($argType *)input completion:($ callbackType )completion ' );
218235 indent.scoped ('{' , '}' , () {
219236 indent.writeln ('FlutterBasicMessageChannel *channel =' );
220237 indent.inc ();
@@ -227,10 +244,14 @@ void _writeFlutterApiSource(Indent indent, ObjcOptions options, Api api) {
227244 indent.writeln ('NSDictionary* inputMap = [input toMap];' );
228245 indent.write ('[channel sendMessage:inputMap reply:^(id reply) ' );
229246 indent.scoped ('{' , '}];' , () {
230- indent.writeln ('NSDictionary* outputMap = reply;' );
231- indent
232- .writeln ('$returnType * output = [$returnType fromMap:outputMap];' );
233- indent.writeln ('completion(output, nil);' );
247+ if (func.returnType == 'void' ) {
248+ indent.writeln ('completion(nil);' );
249+ } else {
250+ indent.writeln ('NSDictionary* outputMap = reply;' );
251+ indent.writeln (
252+ '$returnType * output = [$returnType fromMap:outputMap];' );
253+ indent.writeln ('completion(output, nil);' );
254+ }
234255 });
235256 });
236257 }
0 commit comments