Quickstart
If you like to setup in one command:
cargo install flutter_rust_bridge_codegen && flutter_rust_bridge_codegen create my_app && cd my_app && flutter run
1. Install
After Flutter and Rust are
installed,
install flutter_rust_bridge
using any method:
- Default
- Cargo-Binstall
cargo install flutter_rust_bridge_codegen
cargo binstall flutter_rust_bridge_codegen
2. Create new projects / Add to existing projects
- Create new
- Add to existing
- Pure dart
- Flutter Package
- More approaches
Suppose your app is to be named my_app
, then execute this. (Use --help
to see more options, e.g. package
name)
flutter_rust_bridge_codegen create my_app
Execute the following in the root folder of your Flutter project:
flutter_rust_bridge_codegen integrate
Please refer to this page.
Execute the following command to create a shareable Flutter package that can be used across multiple Flutter applictions:
flutter_rust_bridge_codegen create my_package --template plugin
Please visit this page.
Remark: Directory structure
Though seemingly many files, it is pretty simple:
"a standard Flutter app + a standard Rust crate + some glues to ignore".
In short, write Flutter near lib/main.dart
and Rust near rust/src/api/simple.rs
.
For more details, please see this page.
3. Run it
- Default
- Web
Use your favorite method to run the app (Flutter official doc), as if it is just a normal Flutter project. For example:
flutter run
P.S. The build-web
command: Because Flutter Web does not have a build hook yet
(corresponding issue).
flutter_rust_bridge_codegen build-web
# ... or any other standard Flutter ways
flutter run --web-header=Cross-Origin-Opener-Policy=same-origin --web-header=Cross-Origin-Embedder-Policy=require-corp
Then, you will see a greeting from Rust, displayed in Flutter (Dart).
4. Modify it
Suppose we add a super-simple Rust function in rust/src/api/simple.rs
(see next chapter for all features):
pub fn hello(a: String) -> String { a.repeat(2) }
With all glue code automatically generated, we can call it in Dart (lib/main.dart
):
var result = await hello(a: "Hi");
Explain the Dart code
- The
await
is for asynchronous code, a very frequently used feature in Dart. - To display the result on the screen, a bit of standard Flutter knowledge may be needed. See the existing code for an example how a String can be shown.
We need to execute the code generator whenever the Rust code is changed,
or use --watch
to automatically re-generate when code changes:
flutter_rust_bridge_codegen generate --watch
What's next
On one hand, if you like to see a live demo, please visit the next page.
On the other hand, the guides chapter introduces all features, customizations, common scenario how-tos, etc. There are a lot of documentations, but there is no need to learn all in details. Instead:
Use intuition for flutter_rust_bridge - the ideal bridge between Rust and Dart should be seamless, just like using one single language.
Refer to documentation when curious to know more details, or when some syntax boilerplates are needed.