Flutter 3.10.4 Windows构建EXE偶现后台无界面启动问题求助
Flutter + Rust集成Windows端:双击.exe随机后台启动无界面问题
问题描述
我正在将Flutter作为UI层、Rust作为核心层进行集成,功能可正常运行,但双击生成的.exe文件时,程序有时仅在后台启动却不显示界面。使用flutter run --release启动程序时一切正常,直接打开exe文件则会随机出现后台无界面的情况,多次尝试后无需杀死旧进程即可正常启动。注:我仅关注Windows原生目标平台。
代码实现
main.dart
import 'package:flutter/material.dart'; import 'ffi.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'My First Flutter App'), debugShowCheckedModeBanner: false, ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { // counter int _counter = 0; // increment counter Future<void> _incrementCounter() async { int result = await api.rootskyAdd(left: _counter, right: 2); setState(() { _counter = result; // _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.deepPurple, body: Center( child: Container( margin: const EdgeInsets.all(20), padding: const EdgeInsets.all(20), decoration: BoxDecoration( border: Border.all( color: Colors.white, width: 2, ), ), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', style: TextStyle(color: Colors.white), ), const Text( 'Tell my about yourself.', style: TextStyle(color: Colors.white), ), Text( '$_counter', style: TextStyle(color: Colors.white), ), const SizedBox(height: 20), ElevatedButton( onPressed: _incrementCounter, child: const Text('Increment'), ), ], ), ), ), ); } }
ffi.dart
// This file initializes the dynamic library and connects it with the stub // generated by flutter_rust_bridge_codegen. import 'dart:ffi'; import 'bridge_generated.dart'; import 'bridge_definitions.dart'; export 'bridge_definitions.dart'; // Re-export the bridge so it is only necessary to import this file. export 'bridge_generated.dart'; import 'dart:io' as io; const _base = 'native'; // On MacOS, the dynamic library is not bundled with the binary, // but rather directly **linked** against the binary. final _dylib = io.Platform.isWindows ? '$_base.dll' : 'lib$_base.so'; final Native api = NativeImpl(io.Platform.isIOS || io.Platform.isMacOS ? DynamicLibrary.executable() : DynamicLibrary.open(_dylib));
ffi.dart文件直接复制自flutter_rust_bridge模板。
Flutter环境信息
> flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.10.4, on Microsoft Windows [Version 10.0.19045.2846], locale en-US) [√] Windows Version (Installed version of Windows is version 10 or higher) [!] Android toolchain - develop for Android devices (Android SDK version 33.0.2) X cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.6.2) [!] Android Studio (not installed) [√] IntelliJ IDEA Ultimate Edition (version 2023.1) [√] VS Code (version 1.78.2) [√] Connected device (3 available) [√] Network resources ! Doctor found issues in 2 categories.
已知相关情况
Flutter 3.10.2及更早版本也存在类似问题,有用户提到Android平台在3.10.3版本中已修复该问题。我尚未找到问题原因。
内容的提问来源于stack exchange,提问作者Root Sky 1
相关产品推荐
相关产品推荐

