dart_cpp_bridge 2.2.0
dart_cpp_bridge: ^2.2.0 copied to clipboard
Dart ↔ C++20 bridge (FRB-like). Dart FFI session layer; Native Assets build hooks for Android, iOS, Linux, macOS, and Windows.
dart_cpp_bridge #
Dart/Flutter ↔ C++20 binding generator, inspired by flutter_rust_bridge.
Write normal C++ code and call it from Dart/Flutter, with sync, async, stream, and Dart-closure reverse calls.
What's this? #
- Write plain C++20 functions and classes — no hand-written FFI boilerplate
- Codegen generates Dart API + C++ wire dispatch from annotated headers
- Built-in runtime based on Asio + stdexec senders and coroutines
- Supports Android, iOS, Windows, Linux, and macOS
Runtime model and compatibility #
The package uses the stdexec-based runtime. It contains a broad C++ async/concurrency migration from the legacy async-simple model. The Dart API, C ABI, wire protocol, generated file layout, and method-ID contracts remain stable, but C++ async business code is not source-compatible with the legacy runtime. See the version guide before copying C++ async examples or migrating existing code.
When migrating from the legacy runtime:
- Use
stdexec::task<T>or another stdexec sender instead ofasync_simple::coro::Lazy<T>. - Migrate
Executor/.via(...)andSignal/Slotto stdexec schedulers, current sender operations, and stop tokens. - Regenerate bindings with the matching
dcb_gen_toolrelease; do not mix legacy generated native code with current runtime headers or tooling. - Build native code as C++20 and link the vendored stdexec target. Keep blocking work off the single-threaded I/O scheduler.
Quickstart #
See the documentation for a complete quickstart:
- English: https://deretame.github.io/dart_cpp_bridge/getting-started/
- 中文: https://deretame.github.io/dart_cpp_bridge/zh-cn/getting-started/
Show me the code #
C++
#include <dart_cpp_bridge/annotate.h>
#include <stdexec/execution.hpp>
#include <string>
BRIDGE_SYNC int32_t add(int32_t a, int32_t b) { return a + b; }
BRIDGE_ASYNC
stdexec::task<std::string> greet(std::string name) {
co_return "Hello, " + name;
}
Dart
import 'package:my_app/src/native_gen/api/api_fn.dart';
void main() async {
await DcbLib.init();
print(add(a: 1, b: 2)); // 3
print(await greet(name: 'World')); // Hello, World
}
Documentation #
- Homepage: https://deretame.github.io/dart_cpp_bridge/
- Quickstart: https://deretame.github.io/dart_cpp_bridge/getting-started/
- GitHub: https://github.com/deretame/dart_cpp_bridge
Acknowledgments #
- Flutter Rust Bridge — architecture and product shape inspiration
- Asio — event loop and asynchronous I/O
- stdexec — sender/receiver and scheduler foundation
- MPMCQueue — lock-free bounded MPMC queue (strict FIFO), the buffer of
co::mpsc::boundedchannels - Dart / Flutter team — FFI, Isolate, NativeFinalizer, and the broader Dart native ecosystem
License #
MIT — see LICENSE.