@@ -53,44 +53,50 @@ void _writeHostApi(Indent indent, Api api) {
53
53
'new BasicMessageChannel<Object>(binaryMessenger, "$channelName ", new StandardMessageCodec());' );
54
54
indent.dec ();
55
55
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 {' , () {
59
58
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) ' );
75
63
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 = '' ;
80
69
} else {
81
- indent.writeln ('$returnType output = $call ;' );
82
70
indent.writeln (
83
- 'wrapped.put("${Keys .result }", output.toMap());' );
71
+ '$argType input = $argType .fromMap((HashMap)message);' );
72
+ methodArgument = 'input' ;
84
73
}
85
- });
86
- indent.write ('catch (Exception exception) ' );
87
- indent.scoped ('{' , '}' , () {
88
74
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);' );
90
94
});
91
- indent.writeln ('reply.reply(wrapped);' );
92
95
});
93
96
});
97
+ indent.scoped (null , '}' , () {
98
+ indent.writeln ('channel.setMessageHandler(null);' );
99
+ });
94
100
});
95
101
}
96
102
});
0 commit comments