fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I am a newcomer who just learned rust, how do I create RustOpaque <RwLock<Client>>?

mdddj opened this issue · comments

commented

I found this function in the documentation, but I don't know how to create and return it
image
my code

use std::sync::RwLock;

use aria2_ws::Client;

use crate::frb_generated::{RustAutoOpaque, RustOpaque};

pub enum ConnectError {
    Fail,
}

pub async fn connect_aria2_client(url: String, token: Option<&str>) -> RustOpaque<RwLock<Client>> {
    let client = Client::connect(&url, token).await.unwrap();
    return RustOpaque::new(RwLock::new(client));
}

This is its error message.
image

Hi! Thanks for opening your first issue here! 😄

Hi, usually you do not need to construct it manually. The https://cjycode.com/flutter_rust_bridge/guides/types/arbitrary/rust-auto-opaque is often enough. Do you have special needs for it?

commented

Thanks for your reply, I understand now, I modified it, wrote a simple function, and then ran the command flutter_rust_bridge_codegen generate --watch, which automatically produced the code encountered an error
image
mod.ts

pub mod aria;
pub mod simple;
use aria2_ws::Client;

///connect aria2 server
pub async fn connect_aria2_client(url: String, token: Option<&str>) -> Client {
    let client = Client::connect(&url, token).await.unwrap();
    client
}

simple.rs

#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
pub fn greet(name: String) -> String {
    format!("Hello, {name}!")
}

#[flutter_rust_bridge::frb(init)]
pub fn init_app() {
    // Default utilities - feel free to customize
    flutter_rust_bridge::setup_default_user_utils();
}
image image
commented

dependency version

flutter_rust_bridge = "=2.0.0-dev.28"

Hi, maybe try to pub use aria2_ws::Client;

commented

Thanks, that's solved now.

You are welcome!