@@ -440,11 +440,13 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
440440 ? ''
441441 : _nullsafeKotlinTypeForDartType (method.returnType);
442442
443+ final String resultType =
444+ method.returnType.isVoid ? 'Unit' : returnType;
443445 addDocumentationComments (
444446 indent, method.documentationComments, _docCommentSpec);
445447
446448 if (method.isAsynchronous) {
447- argSignature.add ('callback: ($ returnType ) -> Unit' );
449+ argSignature.add ('callback: (Result<$ resultType > ) -> Unit' );
448450 indent.writeln ('fun ${method .name }(${argSignature .join (', ' )})' );
449451 } else if (method.returnType.isVoid) {
450452 indent.writeln ('fun ${method .name }(${argSignature .join (', ' )})' );
@@ -501,43 +503,54 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
501503 indent.write ('channel.setMessageHandler ' );
502504 indent.addScoped ('{ $messageVarName , reply ->' , '}' , () {
503505 indent.writeln ('var wrapped = listOf<Any?>()' );
504- indent.write ('try ' );
505- indent.addScoped ('{' , '}' , () {
506- final List <String > methodArgument = < String > [];
507- if (method.arguments.isNotEmpty) {
508- indent.writeln ('val args = message as List<Any?>' );
509- enumerate (method.arguments, (int index, NamedType arg) {
510- final String argName = _getSafeArgumentName (index, arg);
511- final String argIndex = 'args[$index ]' ;
512- indent.writeln (
513- 'val $argName = ${_castForceUnwrap (argIndex , arg .type , root )}' );
514- methodArgument.add (argName);
515- });
516- }
517- final String call =
518- 'api.${method .name }(${methodArgument .join (', ' )})' ;
519- if (method.isAsynchronous) {
520- indent.write ('$call ' );
521- final String resultValue =
522- method.returnType.isVoid ? 'null' : 'it' ;
523- indent.addScoped ('{' , '}' , () {
524- indent.writeln ('reply.reply(wrapResult($resultValue ))' );
506+ final List <String > methodArguments = < String > [];
507+ if (method.arguments.isNotEmpty) {
508+ indent.writeln ('val args = message as List<Any?>' );
509+ enumerate (method.arguments, (int index, NamedType arg) {
510+ final String argName = _getSafeArgumentName (index, arg);
511+ final String argIndex = 'args[$index ]' ;
512+ indent.writeln (
513+ 'val $argName = ${_castForceUnwrap (argIndex , arg .type , root )}' );
514+ methodArguments.add (argName);
515+ });
516+ }
517+ final String call =
518+ 'api.${method .name }(${methodArguments .join (', ' )})' ;
519+
520+ if (method.isAsynchronous) {
521+ indent.write ('$call ' );
522+ final String resultType = method.returnType.isVoid
523+ ? 'Unit'
524+ : _nullsafeKotlinTypeForDartType (method.returnType);
525+ indent.addScoped ('{ result: Result<$resultType > ->' , '}' ,
526+ () {
527+ indent.writeln ('val error = result.exceptionOrNull()' );
528+ indent.writeScoped ('if (error != null) {' , '}' , () {
529+ indent.writeln ('reply.reply(wrapError(error))' );
530+ }, addTrailingNewline: false );
531+ indent.addScoped (' else {' , '}' , () {
532+ if (method.returnType.isVoid) {
533+ indent.writeln ('reply.reply(wrapResult(null))' );
534+ } else {
535+ indent.writeln ('val data = result.getOrNull()' );
536+ indent.writeln ('reply.reply(wrapResult(data))' );
537+ }
525538 });
526- } else if (method.returnType.isVoid) {
527- indent. writeln (call);
528- indent.writeln ( 'wrapped = listOf<Any?>(null) ' );
529- } else {
530- indent. writeln ( 'wrapped = listOf<Any?>($ call )' );
531- }
532- }, addTrailingNewline : false );
533- indent. add ( ' catch (exception: Error) ' );
534- indent.addScoped ( '{' , '}' , () {
535- indent. writeln ( 'wrapped = wrapError(exception)' );
536- if (method.isAsynchronous) {
537- indent.writeln ( 'reply.reply(wrapped) ' );
538- }
539- } );
540- if ( ! method.isAsynchronous) {
539+ });
540+ } else {
541+ indent.write ( 'try ' );
542+ indent. addScoped ( '{' , '}' , () {
543+ if (method.returnType.isVoid) {
544+ indent. writeln (call);
545+ indent. writeln ( 'wrapped = listOf<Any?>(null)' );
546+ } else {
547+ indent.writeln ( 'wrapped = listOf<Any?>($ call )' );
548+ }
549+ }, addTrailingNewline : false );
550+ indent.add ( ' catch (exception: Error) ' );
551+ indent. addScoped ( '{' , '}' , () {
552+ indent. writeln ( 'wrapped = wrapError(exception)' );
553+ });
541554 indent.writeln ('reply.reply(wrapped)' );
542555 }
543556 });
0 commit comments