flutter_platform_channels_plus
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
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- 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
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: Contact Developer
Made with β€οΈ by Dhia Bechattaoui
Libraries
- flutter_platform_channels_plus
- flutter_platform_channels_plus