dual_framework 0.0.4
Dual Framework package for cross framework communication.
dual_framework
The Dual Framework package provides a communication layer for establishing interactions between Flutter (the target framework) and external frameworks.
The primary goal is to allow and simplify a modular technological transition from other frameworks to Flutter.
It has been successfully tested for communication between Flutter and Ionic/Angular.
The package supports both mobile (Android and iOS) and web (Browser Chrome) platforms.
Getting started
Prerequisites
- get_it: to register the DualFrameworkService as singleton at app start-up.
Process
At your start-up you register the service:
void setup() {
final locator = GetIt.instance;
locator.registerSingleton<DualFrameworkService>(DualFrameworkService(
configuration: DualFrameworkConfiguration(
origin: "origin url",
webviewUrl: "webview url",
),
));
}
Communication Layer
The communication between the two frameworks is done by sending messages which are listened to and interpreted by the service.
Messages
A message (DualFrameworkMessage) is formed by a command, [command], and additional information, [data].
class DualFrameworkMessage {
final String command;
final String? data;
}
To have cleaner code, using commands of type Enum are advised (using the getter .name to get its String form).
Communication service
The DualFrameworkService service is the one responsible to handle the communication between Flutter and the other framework. It is a Injectable singleton class.
Inside it you can find:
- methods to send messages between the two frameworks
- messages listeners
- configurations for communication
and other additional information.
Webviews
Before starting the communication, we need to create the webview which will be the host of the communication itself.
The parent class DualFrameworkWebview has the following properties:
id: unique id for the webviewframeId(web only): frame id of the webviewactions: list of commands with their callbackscontroller: in app webview controller instancelistenerSubscription(web only): subscription of the listen event for listeners, to be cancelled after webview disposal.
For cleaner implementation, each webview created should have its class and extend this parent class. This is to ensure that each webview has its own unique id to be distinguished from the others.
Here's an example:
class MyNewWebview extends DualFrameworkWebview {
static const String webviewId = "my_new_webview";
MyNewWebview({
String? id,
required super.controller,
required super.actions,
}) : super(
id: webviewId,
);
}
Setting the listeners
Listeners are set to be able to receive messages. The listener is set at webview creation on the onWebViewCreated: (controller) {} callback, from which we retrieve the controller instance.
So we need to create the webview instance and then pass it to the service, that will add it to the active webviews, handling the communication from/to Flutter.
var webview = MyNewWebview(
controller: controller,
actions: ActionMap(
actions: {
DualFrameworkCommand.myAction: (data) => onMyAction(data),
},
),
);
GetIt.instance.get<DualFrameworkService>().createWebview(webview as DualFrameworkWebview);
The listener is platform specific, so different between mobile and web:
- On web, the
window.onMessage.listenmethod is used, from the package dart:html; - On mobile, a javascriptHandler is used, called
'handlerFlutter'from the package flutter_inappwebview.
They both have a callback where the method executeAction() is called that, based on the message received, decides what to do.
Sending messages
To send messages from Flutter to an external framework, there is a method called sendMessageToWebview(), which is also platform specific like the listeners. The listeners outside Flutter will be the ones responsible to read the DualFrameworkMessage instances and decide what to do on their side.
GetIt.instance.get<DualFrameworkService>().sendMessageToWebview(
id: MyNewWebview.webviewId,
message: DualFrameworkMessage(
command: DualFrameworkCommand.anotherAction.name,
data: result,
),
)
- On web, the method
element.contentWindow?.postMessage(DualFrameworkMessage message)is used from the package dart:html; - On mobile, the method
controller.evaluateJavascript()is used from the package flutter_inappwebview, in which the parameterDualFrameworkMessage messageis inserted in a legible form from Javascript.