Skip to main content

Quickstart

info

If you like to setup in one command:

cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0' && 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:

cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0'

2. Create new projects / Add to existing projects

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

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

Use your favorite method to run the app (Flutter official doc), as if it is just a normal Flutter project. For example:

flutter run

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:

tip

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.