flutter_platform_channels_plus

Pub Version Flutter Version Dart Version License

A Flutter plugin that provides simplified platform channel communication with support for all 6 platforms (iOS, Android, Web, Windows, macOS, Linux) and WASM compatibility.

✨ Features

  • πŸ”„ Method Channels: Simple method invocation between Flutter and native code
  • πŸ“‘ Event Channels: Real-time communication with native platforms
  • πŸ’¬ Basic Message Channels: Raw message passing with custom codecs
  • πŸ” Platform Detection: Runtime platform identification and capability checking
  • ⚑ Error Handling: Comprehensive error management with custom exceptions
  • πŸ›‘οΈ Type Safety: Full null safety support and generic type handling
  • βš™οΈ Configuration: Flexible configuration options for timeouts, retries, and logging
  • 🌐 WASM Support: WebAssembly compatibility for web platform

πŸš€ Supported Platforms

Platform Support Native Code WASM
iOS βœ… Full βœ… Yes ❌ No
Android βœ… Full βœ… Yes ❌ No
Web βœ… Full ❌ No βœ… Yes
Windows βœ… Full βœ… Yes ❌ No
macOS βœ… Full βœ… Yes ❌ No
Linux βœ… Full βœ… Yes ❌ No

πŸ“¦ Installation

Add this dependency to your pubspec.yaml:

dependencies:
  flutter_platform_channels_plus: ^0.0.1

Then run:

flutter pub get

🎯 Quick Start

Basic Usage

import 'package:flutter_platform_channels_plus/flutter_platform_channels_plus.dart';

// Create a method channel
final channel = MethodChannelPlus('my_channel');

// Invoke a method
final result = await channel.invokeMethod('getData');
if (result.isSuccess) {
  print('Data: ${result.data}');
} else {
  print('Error: ${result.error}');
}

Event Channels

// Create an event channel
final eventChannel = EventChannelPlus('sensor_data');

// Listen to events
eventChannel.receiveBroadcastStream().listen((event) {
  print('Received sensor data: $event');
});

Message Channels

// Create a message channel for text
final messageChannel = BasicMessageChannelPlus.forText('chat');

// Send a message
await messageChannel.send('Hello, World!');

// Send and receive
final response = await messageChannel.sendAndReceive('ping');
print('Response: $response'); // Output: pong

πŸ”§ Configuration

Channel Configuration

final config = ChannelConfig(
  timeout: Duration(seconds: 30),
  retryCount: 3,
  enableLogging: true,
);

final channel = MethodChannelPlus('my_channel', config: config);

Pre-configured Channels

// High-frequency events (optimized for performance)
final sensorChannel = EventChannelPlus.forHighFrequency('sensors');

// Debug mode (with logging and longer timeouts)
final debugChannel = EventChannelPlus.forDebug('debug');

// Text-based communication
final textChannel = BasicMessageChannelPlus.forText('messages');

// Binary data communication
final binaryChannel = BasicMessageChannelPlus.forBinary('files');

πŸ“š API Reference

MethodChannelPlus

The enhanced method channel with better error handling and configuration.

class MethodChannelPlus {
  // Basic method invocation
  Future<ChannelResult<dynamic>> invokeMethod(String method, [dynamic arguments]);
  
  // Strict method invocation (throws on error)
  Future<T> invokeMethodStrict<T>(String method, [dynamic arguments]);
  
  // Method invocation with retry logic
  Future<ChannelResult<dynamic>> invokeMethodWithRetry(String method, [dynamic arguments, int? maxRetries]);
  
  // Method invocation with timeout
  Future<ChannelResult<dynamic>> invokeMethodWithTimeout(String method, [dynamic arguments, Duration? timeout]);
}

EventChannelPlus

Real-time communication channel for events and streams.

class EventChannelPlus {
  // Broadcast stream (multiple listeners)
  Stream<dynamic> receiveBroadcastStream([dynamic arguments]);
  
  // Single subscription stream
  Stream<dynamic> receiveStream([dynamic arguments]);
  
  // Send event to platform
  Future<ChannelResult<void>> sendEvent(dynamic event);
}

BasicMessageChannelPlus

Raw message passing with custom codecs.

class BasicMessageChannelPlus<T> {
  // Send message
  Future<ChannelResult<void>> send(T message);
  
  // Send and receive
  Future<ChannelResult<T>> sendAndReceive(T message);
  
  // Set message handler
  Future<ChannelResult<void>> setMessageHandler(Future<T?> Function(T? message)? handler);
}

Platform Detection

class PlatformDetector {
  // Current platform
  static PlatformType get currentPlatform;
  
  // Platform capabilities
  static bool get isWeb;
  static bool get isMobile;
  static bool get isDesktop;
  static bool get supportsNativeCode;
  static bool get supportsWasm;
  
  // Check specific capability
  static bool supportsCapability(String capability);
}

πŸ§ͺ Testing

Run the test suite:

flutter test

Run with coverage:

flutter test --coverage

πŸ“– Examples

Check out the example/ directory for complete usage examples:

  • Basic Example: Simple method channel usage
  • Event Example: Real-time event handling
  • Message Example: Custom message codecs
  • Platform Example: Platform detection and capabilities

πŸ” Platform Analysis

This plugin has been analyzed with:

  • βœ… Pana: Full score analysis
  • βœ… Flutter Analyze: Code quality checks
  • βœ… Dart Analysis: Language compliance
  • βœ… Dry Publish: Pre-publication validation

Target Score: 160/160 🎯

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Flutter team for the excellent platform channel architecture
  • The Dart community for continuous improvements
  • Contributors and users of this plugin

πŸ“ž Support


Made with ❀️ by Dhia Bechattaoui

Libraries

flutter_platform_channels_plus
flutter_platform_channels_plus