@@ -53,44 +53,50 @@ void _writeHostApi(Indent indent, Api api) {
5353 'new BasicMessageChannel<Object>(binaryMessenger, "$channelName ", new StandardMessageCodec());' );
5454 indent.dec ();
5555 indent.dec ();
56- indent.write (
57- 'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() ' );
58- indent.scoped ('{' , '});' , () {
56+ indent.write ('if (api != null) ' );
57+ indent.scoped ('{' , '} else {' , () {
5958 indent.write (
60- 'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) ' );
61- indent.scoped ('{' , '}' , () {
62- final String argType = method.argType;
63- final String returnType = method.returnType;
64- String methodArgument;
65- if (argType == 'void' ) {
66- methodArgument = '' ;
67- } else {
68- indent.writeln (
69- '$argType input = $argType .fromMap((HashMap)message);' );
70- methodArgument = 'input' ;
71- }
72- indent.writeln (
73- 'HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();' );
74- indent.write ('try ' );
59+ 'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() ' );
60+ indent.scoped ('{' , '});' , () {
61+ indent.write (
62+ 'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) ' );
7563 indent.scoped ('{' , '}' , () {
76- final String call = 'api.${method .name }($methodArgument )' ;
77- if (method.returnType == 'void' ) {
78- indent.writeln ('$call ;' );
79- indent.writeln ('wrapped.put("${Keys .result }", null);' );
64+ final String argType = method.argType;
65+ final String returnType = method.returnType;
66+ String methodArgument;
67+ if (argType == 'void' ) {
68+ methodArgument = '' ;
8069 } else {
81- indent.writeln ('$returnType output = $call ;' );
8270 indent.writeln (
83- 'wrapped.put("${Keys .result }", output.toMap());' );
71+ '$argType input = $argType .fromMap((HashMap)message);' );
72+ methodArgument = 'input' ;
8473 }
85- });
86- indent.write ('catch (Exception exception) ' );
87- indent.scoped ('{' , '}' , () {
8874 indent.writeln (
89- 'wrapped.put("${Keys .error }", wrapError(exception));' );
75+ 'HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();' );
76+ indent.write ('try ' );
77+ indent.scoped ('{' , '}' , () {
78+ final String call = 'api.${method .name }($methodArgument )' ;
79+ if (method.returnType == 'void' ) {
80+ indent.writeln ('$call ;' );
81+ indent.writeln ('wrapped.put("${Keys .result }", null);' );
82+ } else {
83+ indent.writeln ('$returnType output = $call ;' );
84+ indent.writeln (
85+ 'wrapped.put("${Keys .result }", output.toMap());' );
86+ }
87+ });
88+ indent.write ('catch (Exception exception) ' );
89+ indent.scoped ('{' , '}' , () {
90+ indent.writeln (
91+ 'wrapped.put("${Keys .error }", wrapError(exception));' );
92+ });
93+ indent.writeln ('reply.reply(wrapped);' );
9094 });
91- indent.writeln ('reply.reply(wrapped);' );
9295 });
9396 });
97+ indent.scoped (null , '}' , () {
98+ indent.writeln ('channel.setMessageHandler(null);' );
99+ });
94100 });
95101 }
96102 });
0 commit comments