FBR is a tool that allows to use Flutter and Rust together. It provides a bridge between the two languages. It does not enforce any pardigm, it simply offers to call rust functions from dart and vice versa.
Crux has the same goal of enabling the use of Rust for the business logic, but doesn't support Flutter (yet) as a Frontend. Instead it suggest to use the platforms native UI. Crux is oppinionated in that it requres an event driven approach. However, I like this a lot, thus I implemented it in this example project.
CQRS is a pattern that allows to seperate the business logic from the UI. This is how I refined the Event-Driven appraoch: I split the Events into Commands (for Events that change the state) and Queries (that don't). Both return Effects for side effects, that need to be executed on the shell. Though these are limited to Render effects in case of Queries. The Commands can return Render Effects as well (which is against the strict CQRS definition). This is done so that we don't have to take care of the order (as a Command and a Query for the expected result could be returned in any order).
Since a ViewModel takes care of processing inputs from the UI, we don't need it: Any input from the UI is converted to a Command or a Query by the StateHandler.dart
, which takes care of processing the Effects and updates values the UI listens to as well (which are kind of ViewModel instances).
In general: read the manual
The needed toolchain is installed via nix (flake). Activate with direnv allow
or nix develop
.
Nix (flake) installs the toolchain, which includes the environment variables such (as ANDROID_SDK_ROOT) only in the cli. TO have these available, start codium from a flake directory.
Follow the Quickstart run ''' cargo install flutter_rust_bridge_codegen
cargo binstall flutter_rust_bridge_codegen '''
#### Android
'''
rustup target add
aarch64-linux-android
armv7-linux-androideabi
x86_64-linux-android
i686-linux-android
'''
#### iOS
'''
rustup target add aarch64-apple-ios x86_64-apple-ios
rustup target add aarch64-apple-ios-sim
rustup target add armv7-apple-ios i386-apple-ios '''
run the command ''' just gen '''
If you want to start a new flutter project, do that with ''' flutter_rust_bridge_codegen create --rust-crate-name app_core --rust-crate-dir ../app_core shell_flutter '''
run ''' flutter run ''' in the flutter directory. This compiles the rust lib and binds it as well.
To properly log across devices, the os_log crate is used. The configuration in app_core is enough, so logs can be writte nfrom a shell (see shell_cli for an example).
The log messages can be found via comand line or Console.app
'log stream --predicate="subsystem contains 'com.example.todo_app'" --level debug'
will show the log output in real-time.
'--level' is needed to set the log level, the default is "info". Note that "debug" is not Rust's logging system's debug level, but outputs "trace" log messages as well.
The '--predicate' "subsystem contains 'com.example.todo_app'" filters for whichever string you provided in the constructor of the logger (OsLogger::new("com.example.todo_app")
).
One could filter for the process, like:
log stream --process=app_core --level debug
but this depends on how the app was started:
"app_core" if started from 'app_core/src/main.rs' or "shell_cli" if started from 'shell_cli/src/main.rs'.
One can filter for a spcific log message using the predicate: ''' log stream --predicate 'eventMessage contains "Persisted"' ''' Filters all log messages for the ones containing the word "Persisted".
In the Console.app one needs to apply similar filters. On the left pannel select your device. In the search filed in the upper-left corner one can enter ''' subsystem:com.example.todo_app ''' Or ''' process:app_core ''' to filter for the executed app.
The log level can be changed by the menu Action -> Include Debug Messages
You can use either the command line or Console.app:
Use the exact same command as above. If not filtering for the subsystem but the process the name is Runner
.
Exactly like above, but select the Simulator in "Devices" on the left pannel.