platform_handler 1.1.0 copy "platform_handler: ^1.1.0" to clipboard
platform_handler: ^1.1.0 copied to clipboard

Organize Flutter MethodChannel calls, native callbacks, and request-id based ping-pong responses.

example/example.dart

import 'package:flutter/services.dart';
import 'package:platform_handler/platform_handler.dart';
import 'package:platform_handler/platform_notification.dart';

Future<void> main() async {
  final handler = AppPlatformHandler();
  final logNotification = LogNotification()..listen();
  final ruleNotification = RuleNotification(
    onResult: (result) => print('rule result: $result'),
  )..listen();

  handler.registerChannel('native.demo.com/messageChannel');
  handler.subscribe([
    logNotification,
    ruleNotification,
  ]);

  await handler.invokeMethod('startLog');
  await handler.invokeMethod('luaScript', 'return 1', ruleNotification);
}

class AppPlatformHandler extends PlatformHandler {
  @override
  Future<dynamic> n2fCallDispatcher(MethodCall call) async {
    print('native -> flutter: ${call.method}, ${call.arguments}');
    return super.n2fCallDispatcher(call);
  }
}

class LogNotification extends PlatformNotification {
  LogNotification() {
    subscribers = {
      'LogCallback': onLog,
    };
  }

  void listen() {
    docking = true;
  }

  void onLog(dynamic arguments) {
    print('log: $arguments');
  }
}

class RuleNotification extends PingPongPlatformNotification {
  final void Function(dynamic result) onResult;

  RuleNotification({required this.onResult}) {
    subscribers = {
      'RuleResult': onRuleResult,
    };
  }

  void listen() {
    docking = true;
  }

  void onRuleResult(dynamic responseData) {
    onResult(responseData);
  }
}
1
likes
150
points
110
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Organize Flutter MethodChannel calls, native callbacks, and request-id based ping-pong responses.

Repository (GitHub)
View/report issues

Topics

#platform-channel #method-channel #native-bridge #callbacks #request-id

License

MIT (license)

Dependencies

enhanced_change_notifier, extension_dart, flutter

More

Packages that depend on platform_handler