Skip to content

Commit b887896

Browse files
committed
[pigeon] add null handling to FlutterApi.setup()
Fixes flutter/flutter#66994
1 parent 97c16d5 commit b887896

1 file changed

Lines changed: 40 additions & 31 deletions

File tree

‎packages/pigeon/lib/dart_generator.dart‎

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,42 +88,51 @@ void _writeFlutterApi(Indent indent, Api api,
8888
'BasicMessageChannel<dynamic>(\'$channelName\', StandardMessageCodec());');
8989
indent.dec();
9090
indent.dec();
91-
final String messageHandlerSetter =
91+
indent.write("if (api != null) ");
92+
indent.scoped('{', '}', () {
93+
final String messageHandlerSetter =
9294
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler';
93-
indent
94-
.write('channel.$messageHandlerSetter((dynamic message) async ');
95-
indent.scoped('{', '});', () {
96-
final String argType = func.argType;
97-
final String returnType = func.returnType;
98-
final bool isAsync = func.isAsynchronous;
99-
String call;
100-
if (argType == 'void') {
101-
call = 'api.${func.name}()';
102-
} else {
103-
indent.writeln(
104-
'final Map<dynamic, dynamic> mapMessage = message as Map<dynamic, dynamic>;');
105-
indent.writeln(
106-
'final $argType input = $argType._fromMap(mapMessage);');
107-
call = 'api.${func.name}(input)';
108-
}
109-
if (returnType == 'void') {
110-
indent.writeln('$call;');
111-
if (isMockHandler) {
112-
indent.writeln('return <dynamic, dynamic>{};');
95+
indent
96+
.write('channel.$messageHandlerSetter((dynamic message) async ');
97+
indent.scoped('{', '});', () {
98+
final String argType = func.argType;
99+
final String returnType = func.returnType;
100+
final bool isAsync = func.isAsynchronous;
101+
String call;
102+
if (argType == 'void') {
103+
call = 'api.${func.name}()';
104+
} else {
105+
indent.writeln(
106+
'final Map<dynamic, dynamic> mapMessage = message as Map<dynamic, dynamic>;');
107+
indent.writeln(
108+
'final $argType input = $argType._fromMap(mapMessage);');
109+
call = 'api.${func.name}(input)';
113110
}
114-
} else {
115-
if (isAsync) {
116-
indent.writeln('final $returnType output = await $call;');
111+
if (returnType == 'void') {
112+
indent.writeln('$call;');
113+
if (isMockHandler) {
114+
indent.writeln('return <dynamic, dynamic>{};');
115+
}
117116
} else {
118-
indent.writeln('final $returnType output = $call;');
117+
if (isAsync) {
118+
indent.writeln('final $returnType output = await $call;');
119+
} else {
120+
indent.writeln('final $returnType output = $call;');
121+
}
122+
const String returnExpresion = 'output._toMap()';
123+
final String returnStatement = isMockHandler
124+
? 'return <dynamic, dynamic>{\'${Keys.result}\': $returnExpresion};'
125+
: 'return $returnExpresion;';
126+
indent.writeln(returnStatement);
119127
}
120-
const String returnExpresion = 'output._toMap()';
121-
final String returnStatement = isMockHandler
122-
? 'return <dynamic, dynamic>{\'${Keys.result}\': $returnExpresion};'
123-
: 'return $returnExpresion;';
124-
indent.writeln(returnStatement);
125-
}
128+
});
126129
});
130+
indent.write(" else ");
131+
indent.scoped('{', '}', () {
132+
final String messageHandlerSetter =
133+
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler';
134+
indent.writeln('channel.$messageHandlerSetter(null);');
135+
});
127136
});
128137
}
129138
});

0 commit comments

Comments
 (0)