当我在 Windows 10 x64 上运行 Flutter 应用程序时,该应用程序在使用 cloud_firestore 侦听文档时崩溃。没有显示调试消息。有没有人遇到过这个问题或者知道解决方案? 详情如下。
创建仅适用于windows平台的Flutter项目
flutter create --org com.clipnavi --description 'test' --platforms windows --template app test
添加 Firebase 项目
dart pub global activate flutterfire_cli
flutterfire configure --project=clipspace1
仅检查窗口并输入。
酒吧添加
flutter pub add firebase_core
flutter pub add cloud_firestore
使用 VS Code 打开项目文件夹。
main.dart:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:test/firebase_options.dart';
import 'package:test/my_home_page.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
my_home_page.dart:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
FirebaseFirestore.instance
.collection("status")
.doc("eELQ6vSdSPcgM5Ha3RTyHKtBPBl1")
.snapshots()
.listen(
(document) {
print("No problem 1");
print("current data: ${document.data()}");
},
onError: (error) => print("Listen failed: $error"),
);
}
@override
Widget build(BuildContext context) {
return Center(
child: Text("test"),
);
}
}
检查 Firebase 数据库: Firebase 数据库
颤抖运行
2.30.1
3.19.6
在 Windows 上以调试模式启动 lib\main.dart... CMake 弃用警告位于 C:/Users/olerh/Desktop/windragon/test/build/windows/x64/extracted/firebase_cpp_sdk_windows/CMakeLists.txt:17 (cmake_minimum_required): 与 CMake 的兼容性 < 3.5 will be removed from a future version of CMake.
更新 VERSION 参数值或使用 ... 后缀来告知 CMake认为该项目不需要兼容旧版本。 2
√ 构建build\windows\x64 unner\调试 est.exe。 连接到 VM 服务 ws://127.0.0.1:51213/pc8BnbWb_lk=/ws 与设备的连接丢失。
退出。
奔跑
flutter doctor -v
[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.19045.4291], locale tr-TR)
• Flutter version 3.19.6 on channel stable at C:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e66469a9 (8 days ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:\Users\olerh\AppData\Local\Android\sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.6)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.9.34728.123
• Windows 10 SDK version 10.0.22621.0
[√] Android Studio (version 2023.2)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.9+0--11185874)
[√] VS Code (version 1.88.1)
• VS Code at C:\Users\olerh\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.86.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.4291]
• Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.78
• Edge (web) • edge • web-javascript • Microsoft Edge 124.0.2478.51
[√] Network resources
• All expected network resources are available.
• No issues found!
这对于flutter windows应用来说是一个致命的问题。我有一个更好的解决方案,但这不是实际的解决方案。 Firebase和Flutter的需要就是解决这个问题。现在我的临时解决方案是在调用 runApp() 之前启动应用程序时清除 Fire 存储缓存。这就像:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await clearFirestoreCache();
runApp(const App());
}
Future<void> clearFirestoreCache() async {
try {
await FirebaseFirestore.instance.clearPersistence();
print("Firestore cache cleared successfully.");
} catch (e) {
print("Failed to clear Firestore cache: $e");
}
}
这可能不适用于某些机器。