flutter_module_bridge 0.1.2
flutter_module_bridge: ^0.1.2 copied to clipboard
A production-grade, bidirectional communication bridge between Flutter modules and native Android/iOS applications. Designed with a protocol-first architecture, strong typing, interceptors, capability [...]
flutter_module_bridge #
A production-grade, bidirectional communication bridge between Flutter modules and native Android/iOS applications.
Features #
- ✅ Protocol-first — Every message is a versioned
BridgeEnvelope - ✅ Typed API —
call<LoginResponse>("auth.login", request) - ✅ Capability negotiation — Handshake, version agreement, method discovery
- ✅ Namespace support —
bridge.namespace("auth").call("login") - ✅ Events —
on(),once(),off(),emit() - ✅ Streams — Continuous native data (location, sensors, Bluetooth)
- ✅ Progress — Upload/download progress with
onProgresscallback - ✅ Cancellation —
BridgeCancellationTokenwith native signal - ✅ Queue — Requests submitted before init are automatically flushed
- ✅ Connection states —
disconnected → connecting → handshaking → ready - ✅ Metrics — EMA response time, error rates, active request count
- ✅ Plugins —
HeartbeatPlugin,ReconnectionPlugin, custom plugins - ✅ Middleware —
LoggingMiddleware,RetryMiddleware, custom middleware - ✅ Interceptors — Auth injection, encryption, analytics
- ✅ MockTransport — Full test coverage without a native host
- ✅ Multi-engine — Multiple named instances for
FlutterEngineGroup
Quick Start #
1. Initialize #
import 'package:flutter_module_bridge/flutter_module_bridge.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await NativeBridge.initialize(
config: BridgeConfig(channelName: 'app_bridge'),
);
runApp(const MyApp());
}
2. Register Types #
NativeBridge.registerType<LoginResponse>(
fromJson: LoginResponse.fromJson,
toJson: (r) => r.toJson(),
);
3. Call Native #
final user = await NativeBridge.call<LoginResponse>(
'auth.login',
LoginRequest(email: 'user@example.com', password: '...'),
);
4. Listen to Events #
final sub = NativeBridge.on<UserEvent>('userUpdated', (event) {
print('User updated: ${event.name}');
});
// Cancel when done
sub.cancel();
5. Stream Data #
NativeBridge.stream<LocationEvent>('location').listen((loc) {
print('${loc.lat}, ${loc.lng}');
});
Native Setup #
Android (Kotlin) #
// In your Activity or Application
val bridge = FlutterModuleBridgePlugin.getBridge(flutterEngine)
bridge?.namespace("auth")
?.register<LoginRequest, LoginResponse>("login") { request, _, _ ->
authService.login(request.email, request.password)
}
iOS (Swift) #
// In your AppDelegate or FlutterViewController subclass
guard let bridge = FlutterModuleBridgePlugin.getBridge(for: flutterEngine) else { return }
bridge.namespace("auth")
.register("login", LoginRequest.self, LoginResponse.self) { request, _, _ in
try await authService.login(email: request.email, password: request.password)
}
Documentation #
License #
MIT — see LICENSE.