行业知识
【教程】Flutter与Rust完美交互,无需手写FFI代码
2026-11-10 02:33:52
来源:互联网转载
实践环境:Windows11
flutter_rust_bridge官方文档
Flutter环境配置教程 | Rust环境配置教程
新建一个全新的Flutter项目并运行:
flutter create example && cd example && flutter run
在Flutter项目根目录新建一个Rust项目:
cargo new native --lib
目录结构大概是这样的:
接下来,将这两行添加到 Cargo.toml:
[lib]crate-type = ["staticlib", "cdylib"][dependencies]flutter_rust_bridge = "1.78.0"[build-dependencies]flutter_rust_bridge_codegen = "1.78.0"
在native/src目录新建一个api.rs
添加以下示例代码:
//api.rspub fn hello() -> String { format!("{}", "你好,Rust!")} lib.rs
//lib.rsmod api;
安装flutter_rust_bridge_codegen:
cargo install flutter_rust_bridge_codegen
修改Flutter项目的pubspec.yaml配置文件:
pubspec.yaml 配置文件完整代码:
name: mobiledescription: A new Flutter project.# The following line prevents the package from being accidentally published to# pub.dev using `flutter pub publish`. This is preferred for private packages.publish_to: "none" # Remove this line if you wish to publish to pub.dev# The following defines the version and build number for your application.# A version number is three numbers separated by dots, like 1.2.43# followed by an optional build number separated by a +.# Both the version and the builder number may be overridden in flutter# build by specifying --build-name and --build-number, respectively.# In Android, build-name is used as versionName while build-number used as versionCode.# Read more about Android versioning at https://developer.android.com/studio/publish/versioning# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.# Read more about iOS versioning at# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html# In Windows, build-name is used as the major, minor, and patch parts# of the product and file versions while build-number is used as the build suffix.version: 1.0.0+1environment: sdk: ">=3.0.5
上一篇:梦幻西游怎么卡进服务器?
下一篇:王者荣耀服务器编号怎么看?