Initialize the Flutter App and Setup the Flutter-Rust-Bridge


Watch the video! Step 1: A guide to connect Flutter and Rust for the Simple App Project.


GitHub Repository

πŸ‘‰ Β  GitHub Repo - Simple App (Flutter only)


Steps in the video

In Terminal

git clone https://github.com/iota-for-flutter/simple_app.git
cd simple_app
code .

In VSCode

cargo new --lib rust
cargo install flutter_rust_bridge_codegen

flutter pub add --dev ffigen:9.0.1 && flutter pub add ffi
flutter pub add flutter_rust_bridge
flutter pub add -d build_runner
flutter pub add -d freezed
flutter pub add freezed_annotation

In Cargo.toml

[dependencies]
flutter_rust_bridge = "1"

[lib]
crate-type = ["staticlib", "cdylib"]

Add the Rust Code from the previous chapter.

In Cargo.toml, add these libraries to the existing ones.

[dependencies]
iota-client = { version = "2.0.1-rc.7", default-features = false, features = [ "tls" ] }

tokio = { version = "1.34.0", features = ["full"] }
anyhow = { version = "1.0.75" }
[dependencies]
iota-sdk = { version = "1.1.4", default-features = false, features = [
    "client",
    "tls",
] }

tokio = { version = "1.34.0", features = ["full"] }
anyhow = { version = "1.0.75" }

api.rs

use iota_client::{block::BlockId, Client};
use tokio::runtime::Runtime;
use anyhow::Result;

pub fn publish_tagged_data_block(tag: String, message: String) -> Result<String> {
    let node_url: String = String::from("https://api.shimmer.network");

    let rt = Runtime::new().unwrap();
    rt.block_on(async {
        // Create a client with that node.
        let client = Client::builder().with_node(&node_url)?.finish()?;

        // Create and send the block with tag and data.
        let block = client
            .block()
            .with_tag(tag.into_bytes())
            .with_data(message.into_bytes())
            .finish()
            .await?;

        let block_id:BlockId = block.id();

        Ok(block_id.to_string())
    })
}
use iota_sdk::{
    client::Client,
    types::block::BlockId,
};

use anyhow::Result;
use tokio::runtime::Runtime;

pub fn publish_tagged_data_block(tag: String, message: String) -> Result<String> {
    let node_url: String = String::from("https://api.shimmer.network");

    let rt = Runtime::new().unwrap();
    rt.block_on(async {
        // Create a client with that node.
        let client = Client::builder().with_node(&node_url)?.finish().await?;

        // Create and send the block with tag and data.
        let block = client
            .build_block()
            .with_tag(tag.into_bytes())
            .with_data(message.into_bytes())
            .finish()
            .await?;

        let block_id:BlockId = block.id();

        Ok(block_id.to_string())
    })
}

lib.rs

mod api;