Aus öffentlichen Paketdaten erstellte Orientierung.
dsbridge_flutter ist eine plattformübergreifende JavaScript-Bridge, die nahtlose synchrone oder asynchrone Funktionsaufrufe zwischen JavaScript und Dart in Flutter-Anwendungen ermöglicht. Sie unterstützt Android, iOS und macOS und ermöglicht Entwicklern die Integration von Webinhalten mit native Funktionen. Die Bridge ermöglicht bidirektionale Kommunikation und ist daher ideal für die Entwicklung hybrider Apps, bei denen WebViews mit Native-Code interagieren. Ihr leichtgewichtiges Design und die einfache API vereinfachen die Integration in bestehende Projekte.
Anwendungsfälle
Integration von Webansichten mit nativen Flutter-Funktionen
Echtzeitkommunikation zwischen Web und Dart aktivieren
Teams, die dynamische, interaktive Schnittstellen erstellen
Projekte, die einen Echtzeit-Datenaustausch erfordern
Hinweise
Bei der Exposition von Dart-Funktionen an JavaScript ist besondere Sorgfalt bei der Sicherheit erforderlich
Kann zusätzliche Überprüfung für unvertrauenswürdige JavaScript-Eingaben erfordern
Nicht geeignet für hochfrequente, niedriglatenzige Operationen
Geringe Dokumentation im Vergleich zu reifen Paketen
Mögliche Leistungseinbußen bei großen Datenübertragungen
Der Lebenszyklus von Brücken muss verwaltet werden, um Speicherlecks zu vermeiden
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
DSBridge for Flutter
Modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and Flutter applications.
Dart APIs must be registered with registerFunction in register function.
Note that if you use the --obfuscate option to obfuscate Dart code when building your application, you need to annotate the function with @pragma('vm:entry-point') or use the second parameter functionName of registerFunction to specify the function name.
Namespaces can help you better manage your APIs, which is very useful in hybrid applications, because these applications have a large number of APIs. DSBridge allows you to classify API with namespace. The namespace can be multilevel, between different levels with '.' division.
Debug mode
In debug mode, some errors will be prompted by a popup dialog , and the exception caused by the dart APIs will not be captured to expose problems.
Progress Callback
Normally, when a API is called to end, it returns a result, which corresponds one by one. But sometimes a call need to repeatedly return multiple times, Suppose that on the Flutter side, there is a API to download the file, in the process of downloading, it will send the progress information to JavaScript many times, then JavaScript will display the progress information on the H5 page. Oh...You will find it is difficult to achieve this function. Fortunately, DSBridge supports Progress Callback. You can be very simple and convenient to implement a call that needs to be returned many times. Here's an example of a countdown:
In Dart
voidcallProgress(dynamic args, CompletionHandler handler) {
var i = 10;
final timer = Timer.periodic(constDuration(seconds: 1), (timer) {
if (i == 0) {
timer.cancel();
handler.complete(0);
} else {
handler.setProgressData(i--);
}
});
}
In JavaScript
dsBridge.call("callProgress", function (value) {
document.getElementById("progress").innerText = value
})
For the complete sample code, please refer to the example project.
JavaScript popup box
For JavaScript popup box functions (alert/confirm/prompt), DSBridge has implemented them all by default, if you want to custom them, set the corresponding callback in DWebViewController. The default dialog box implemented by DSBridge is modal. This will block the UI thread.
API Reference
Dart API
In Dart, the object that implements the JavaScript interfaces is called Dart API object.
Call the JavaScript API. If a handler is given, the JavaScript handler can respond. the handlerName can contain the namespace. The handler will be called in Dart main isolate.
Example:
_controller.callHandler('append', args: ["I", "love", "you"],
handler: (retValue) {
print(retValue.toString());
});
/// call with namespace 'syn', More details to see the Demo project
_controller.callHandler('syn.getInfo', handler: (retValue) {
print(retValue.toString());
});
DWebViewController.javaScriptCloseWindowListener
DWebViewController calls callback when JavaScript calls window.closeyou can provide a callback to add your handler.
Test whether the handler exist in Dart, the handlerName can contain the namespace.
type: optional["all"|"syn"|"asyn" ], default is "all".
dsBridge.hasNativeMethod('testAsyn')
//test namespace method
dsBridge.hasNativeMethod('test.testAsyn')
// test if exist a asynchronous function that named "testSyn"
dsBridge.hasNativeMethod('testSyn','asyn') //false
Finally
If you like DSBridge for Flutter, please click star/like to let more people know it, Thanks!