From 03817a995a0d214cf1efeb4384b11c99735c6e70 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 9 Apr 2019 07:22:25 -0700 Subject: [PATCH] fix cast NPE in invokeListMethod and invokeMapMathod (#30760) --- .../lib/src/services/platform_channel.dart | 4 +-- .../test/services/platform_channel_test.dart | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index 51c0dbb301..6fcaca5ada 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -314,7 +314,7 @@ class MethodChannel { /// * [invokeMethod], which this call delegates to. Future> invokeListMethod(String method, [ dynamic arguments ]) async { final List result = await invokeMethod>(method, arguments); - return result.cast(); + return result?.cast(); } /// An implementation of [invokeMethod] that can return typed maps. @@ -328,7 +328,7 @@ class MethodChannel { /// * [invokeMethod], which this call delegates to. Future> invokeMapMethod(String method, [ dynamic arguments ]) async { final Map result = await invokeMethod>(method, arguments); - return result.cast(); + return result?.cast(); } /// Sets a callback for receiving method calls on this channel. diff --git a/packages/flutter/test/services/platform_channel_test.dart b/packages/flutter/test/services/platform_channel_test.dart index 243a254e39..9b1108073e 100644 --- a/packages/flutter/test/services/platform_channel_test.dart +++ b/packages/flutter/test/services/platform_channel_test.dart @@ -69,6 +69,21 @@ void main() { expect(await channel.invokeListMethod('sayHello', 'hello'), ['hello', 'world']); }); + test('can invoke list method and get null result', () async { + BinaryMessages.setMockMessageHandler( + 'ch7', + (ByteData message) async { + final Map methodCall = jsonMessage.decodeMessage(message); + if (methodCall['method'] == 'sayHello') { + return jsonMessage.encodeMessage([null]); + } else { + return jsonMessage.encodeMessage(['unknown', null, null]); + } + }, + ); + expect(await channel.invokeListMethod('sayHello', 'hello'), null); + }); + test('can invoke map method and get result', () async { BinaryMessages.setMockMessageHandler( @@ -86,6 +101,21 @@ void main() { expect(await channel.invokeMapMethod('sayHello', 'hello'), {'hello': 'world'}); }); + test('can invoke map method and get null result', () async { + BinaryMessages.setMockMessageHandler( + 'ch7', + (ByteData message) async { + final Map methodCall = jsonMessage.decodeMessage(message); + if (methodCall['method'] == 'sayHello') { + return jsonMessage.encodeMessage([null]); + } else { + return jsonMessage.encodeMessage(['unknown', null, null]); + } + }, + ); + expect(await channel.invokeMapMethod('sayHello', 'hello'), null); + }); + test('can invoke method and get error', () async { BinaryMessages.setMockMessageHandler( 'ch7',