From cbf7d9887494441895fc6f921acc0fb3f86972ef Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 1 Jul 2016 15:50:29 -0700 Subject: [PATCH] Allow a host message handler to return a null result (#4812) Host apps will return null if there is no matching handler for a message name --- packages/flutter/lib/src/services/host_messages.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/services/host_messages.dart b/packages/flutter/lib/src/services/host_messages.dart index 6f0abe07ba..8c7d1597c2 100644 --- a/packages/flutter/lib/src/services/host_messages.dart +++ b/packages/flutter/lib/src/services/host_messages.dart @@ -57,11 +57,15 @@ class HostMessages { return completer.future; } + static dynamic _decode(String message) { + return message != null ? JSON.decode(message) : null; + } + /// Sends a JSON-encoded message to the host application and JSON-decodes the response. static Future sendJSON(String messageName, [dynamic json]) async { Completer completer = new Completer(); _hostAppMessagesProxy.sendString(messageName, JSON.encode(json), (String reply) { - completer.complete(JSON.decode(reply)); + completer.complete(_decode(reply)); }); return completer.future; } @@ -78,7 +82,7 @@ class HostMessages { /// before being returned to the host application. static void addJSONMessageHandler(String messageName, Future callback(dynamic json)) { _appMessages.handlers[messageName] = (String message) async { - return JSON.encode(await callback(JSON.decode(message))); + return JSON.encode(await callback(_decode(message))); }; } }