fix cast NPE in invokeListMethod and invokeMapMathod (#30760)
This commit is contained in:
@@ -314,7 +314,7 @@ class MethodChannel {
|
||||
/// * [invokeMethod], which this call delegates to.
|
||||
Future<List<T>> invokeListMethod<T>(String method, [ dynamic arguments ]) async {
|
||||
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.
|
||||
@@ -328,7 +328,7 @@ class MethodChannel {
|
||||
/// * [invokeMethod], which this call delegates to.
|
||||
Future<Map<K, V>> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async {
|
||||
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.
|
||||
|
||||
@@ -69,6 +69,21 @@ void main() {
|
||||
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 {
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
@@ -86,6 +101,21 @@ void main() {
|
||||
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 {
|
||||
BinaryMessages.setMockMessageHandler(
|
||||
'ch7',
|
||||
|
||||
Reference in New Issue
Block a user