dsbridge_flutter는 Flutter 애플리케이션에서 JavaScript와 Dart 간의 원활한 동기 또는 비동기 함수 호출을 가능하게 하는 크로스플랫폼 자바스크립트 브리지입니다. Android, iOS, macOS를 지원하며 웹 콘텐츠를 네이티브 기능과 통합할 수 있도록 해줍니다. 양방향 통신을 지원하여 웹뷰가 네이티브 코드와 상호작용하는 하이브리드 앱 개발에 적합합니다. 가벼운 설계와 직관적인 API로 기존 프로젝트에 쉽게 통합할 수 있습니다.
사용 사례
네이티브 플러터 기능과 웹 뷰 통합
웹과 Dart 간 실시간 통신 활성화
웹 컴포넌트를 사용하여 하이브리드 모바일 앱 빌드하기
사용자 정의 자바스크립트에서 Dart 콜백 구현
자바스크립트를 통한 동적 UI 업데이트 지원
웹 기반 로직을 Flutter 비즈니스 로직과 연결하기
주요 기능
양방향 함수 호출
동기 및 비동기 호출
크로스 플랫폼 지원 (Android, iOS, macOS)
경량이며 쉽게 통합할 수 있는 API
안전한 통신 채널
복잡한 데이터 유형을 지원합니다
적합한 대상
하이브리드 앱 개발자
웹 뷰 통합자
JavaScript를 사용하는 Flutter 개발자
밀접한 웹 네이티브 통합을 필요로 하는 앱
동적이고 상호작용적인 인터페이스를 구축하는 팀
실시간 데이터 교환을 필요로 하는 프로젝트
고려 사항
Dart 함수를 JavaScript에 노출할 때 보안 처리에 주의가 필요합니다
신뢰할 수 없는 자바스크립트 입력에 대해 추가 검증이 필요할 수 있습니다
고주파, 저지연 작업에 적합하지 않습니다
성숙한 패키지에 비해 문서가 제한적
대용량 데이터 전송 시 성능 오버헤드 가능성
메모리 누수를 방지하기 위해 브리지의 수명 주기를 관리해야 합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
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!