channel_util.dart 873 字节
Newer Older
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:ui';

import 'package:meta/meta.dart';

/// Util method to replicate the behavior of a `MethodChannel` in the Flutter
/// framework.
void sendJsonMethodCall({
13
  @required Window window,
14 15 16 17 18
  @required String channel,
  @required String method,
  dynamic arguments,
  PlatformMessageResponseCallback callback,
}) {
19
  window.sendPlatformMessage(
20 21 22 23 24 25 26 27 28 29 30 31
    channel,
    // This recreates a combination of OptionalMethodChannel, JSONMethodCodec,
    // and _DefaultBinaryMessenger in the framework.
    utf8.encoder.convert(
      const JsonCodec().encode(<String, dynamic>{
        'method': method,
        'args': arguments,
      })
    ).buffer.asByteData(),
    callback,
  );
}