Unverified Commit 03817a99 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

fix cast NPE in invokeListMethod and invokeMapMathod (#30760)

parent b1611cc9
...@@ -314,7 +314,7 @@ class MethodChannel { ...@@ -314,7 +314,7 @@ class MethodChannel {
/// * [invokeMethod], which this call delegates to. /// * [invokeMethod], which this call delegates to.
Future<List<T>> invokeListMethod<T>(String method, [ dynamic arguments ]) async { Future<List<T>> invokeListMethod<T>(String method, [ dynamic arguments ]) async {
final List<dynamic> result = await invokeMethod<List<dynamic>>(method, arguments); final List<dynamic> result = await invokeMethod<List<dynamic>>(method, arguments);
return result.cast<T>(); return result?.cast<T>();
} }
/// An implementation of [invokeMethod] that can return typed maps. /// An implementation of [invokeMethod] that can return typed maps.
...@@ -328,7 +328,7 @@ class MethodChannel { ...@@ -328,7 +328,7 @@ class MethodChannel {
/// * [invokeMethod], which this call delegates to. /// * [invokeMethod], which this call delegates to.
Future<Map<K, V>> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async { Future<Map<K, V>> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async {
final Map<dynamic, dynamic> result = await invokeMethod<Map<dynamic, dynamic>>(method, arguments); final Map<dynamic, dynamic> result = await invokeMethod<Map<dynamic, dynamic>>(method, arguments);
return result.cast<K, V>(); return result?.cast<K, V>();
} }
/// Sets a callback for receiving method calls on this channel. /// Sets a callback for receiving method calls on this channel.
......
...@@ -69,6 +69,21 @@ void main() { ...@@ -69,6 +69,21 @@ void main() {
expect(await channel.invokeListMethod<String>('sayHello', 'hello'), <String>['hello', 'world']); expect(await channel.invokeListMethod<String>('sayHello', 'hello'), <String>['hello', 'world']);
}); });
test('can invoke list method and get null result', () async {
BinaryMessages.setMockMessageHandler(
'ch7',
(ByteData message) async {
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
if (methodCall['method'] == 'sayHello') {
return jsonMessage.encodeMessage(<dynamic>[null]);
} else {
return jsonMessage.encodeMessage(<dynamic>['unknown', null, null]);
}
},
);
expect(await channel.invokeListMethod<String>('sayHello', 'hello'), null);
});
test('can invoke map method and get result', () async { test('can invoke map method and get result', () async {
BinaryMessages.setMockMessageHandler( BinaryMessages.setMockMessageHandler(
...@@ -86,6 +101,21 @@ void main() { ...@@ -86,6 +101,21 @@ void main() {
expect(await channel.invokeMapMethod<String, String>('sayHello', 'hello'), <String, String>{'hello': 'world'}); expect(await channel.invokeMapMethod<String, String>('sayHello', 'hello'), <String, String>{'hello': 'world'});
}); });
test('can invoke map method and get null result', () async {
BinaryMessages.setMockMessageHandler(
'ch7',
(ByteData message) async {
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
if (methodCall['method'] == 'sayHello') {
return jsonMessage.encodeMessage(<dynamic>[null]);
} else {
return jsonMessage.encodeMessage(<dynamic>['unknown', null, null]);
}
},
);
expect(await channel.invokeMapMethod<String, String>('sayHello', 'hello'), null);
});
test('can invoke method and get error', () async { test('can invoke method and get error', () async {
BinaryMessages.setMockMessageHandler( BinaryMessages.setMockMessageHandler(
'ch7', 'ch7',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment