我试图在 Flutter 应用程序中捕获所有未处理的异常,以便将其发送给崩溃报告者。Flutter 文档中有关于如何执行此操作的说明。我遵循了这些,并在我的应用程序中添加了两段代码以捕获异常:
通过包装runApp来捕捉 Dart 错误runZoned:
runZoned<Future<void>>(
() async {
runApp(MyApp());
},
onError: (dynamic error, StackTrace stackTrace) {
print("=================== CAUGHT DART ERROR");
// Send report
},
);
Run Code Online (Sandbox Code Playgroud)
通过设置捕捉颤振错误FlutterError.onError:
FlutterError.onError = (FlutterErrorDetails details) {
print("=================== CAUGHT FLUTTER ERROR");
// Send report
};
Run Code Online (Sandbox Code Playgroud)
但是,当我在运行时通过从按钮抛出异常进行测试时:
throw Exception("Just testing");
Run Code Online (Sandbox Code Playgroud)
控制台中出现异常:
????????? 异常捕获手势???????????????????????????????????????????????? ????????????????????? 处理手势时抛出了以下 _Exception: Exception: Just testing 当抛出异常时,这是堆栈:
... 等等
但是我看不到我的打印语句的迹象(CAUGHT DART ERROR 或 CAUGHT FLUTTER ERROR),并且在这些行上设置断点似乎永远不会命中,所以我认为我的异常处理代码没有捕捉到它。我错过了什么吗?
这是一个最小的可重现示例(单击按钮,该按钮会引发异常,但未按预期捕获):
import 'dart:async';
import 'package:flutter/material.dart'; …Run Code Online (Sandbox Code Playgroud) 我有 flutter 应用程序,它为每个平台(移动、网络、窗口)使用不同的 webview 插件。
虽然我能够import基于web和建立平台mobile,但我无法导入 Windows。
如果它不是移动或网络,我尝试添加其他条件,但它正在使用mobile插件。
import 'package:eam_flutter/form/mobileui.dart'
if (dart.library.html) 'package:eam_flutter/form/webui.dart'
as multiPlatform;
Run Code Online (Sandbox Code Playgroud)
import 'package:eam_flutter/form/windowui.dart'
if (dart.library.html) 'package:eam_flutter/form/webui.dart'
if (dart.library.io) 'package:eam_flutter/form/mobileui.dart'
as multiPlatform;
Run Code Online (Sandbox Code Playgroud)
如何为 Windows 指定条件导入?
我有一个AlertDialog小部件,当您点击其文本时,它会显示 SnackBar。SnackBar 当前显示AlertDialog在屏障后面的背景中。我希望 Snackbar 显示在透明AlertDialog屏障的顶部。我正在寻求的行为可以在 Flutter 中实现吗?我创建了一个全新的 Flutter 应用程序,仅包含用于说明下面用例的相关代码以及屏幕截图。
Main.dart 要点
@override
Widget build(BuildContext context) {
WidgetsBinding.instance!.addPostFrameCallback((_) async {
showDialog(
context: context,
builder: (BuildContext dialogContext) => AlertDialog(
content: GestureDetector(
onTap: () {
ScaffoldMessenger.of(dialogContext).showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () {},
),
));
},
child: Center(
child: Text('Show SnackBar!'),
),
),
),
);
});
// This method is rerun every time setState is called, for instance as done …Run Code Online (Sandbox Code Playgroud) 我试图理解下面写的代码。初始化方法中参数后面的冒号是什么意思?比如 consumable: account: 等。我知道变量名前有一个冒号意味着它是一个符号,并且不能像变量那样改变它的值。但是后面加冒号是什么意思呢?谢谢
class Purchaser
attr_accessor :consumable, :account, :amount, :reason
def initialize(consumable:, account:, amount:, reason:)
@consumable = consumable
@account = account
@amount = amount
@reason = reason
end
def make_purchase
if purchase.update(account: account, amount: amount, reason: reason) && decrease_stock
return true
else
return false
end
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我希望开关用于在开/关之间切换设置,分别为真/假。当我去构建它时,结果发现flutter提供了一个默认开关,但这不是我想要的,我想根据我的UI对其进行自定义
这是颤振开关按钮:-

这是我想要的:-

我怎样才能让我的 UI 成为可能?
我正在开发 flutter 桌面应用程序。我只想执行应用程序的单个实例。但目前它允许我运行多个实例。如何只允许该应用程序的一个 .exe 文件运行?
我正在dlblack.dev 上慢慢建立我的个人网站,并且我正在尝试为它增添一点趣味。例如,从计算机(而不是平板电脑或手机,因为它们没有鼠标指针),如果您将鼠标悬停在任何可点击的项目上,它不会改变您的鼠标指针以表明它是可点击的,并且可点击的对象根本不会改变。我决定遵循这个 FilledStacks 教程,但它没有提到关于解决这个问题的任何内容。
基本上发生的事情是,当我通过教程视频(他在那里编写骨架扩展类)获得大约 2.5 分钟并尝试复制它时,VS Code 将几乎整个类声明(除了名称)红线。我正在写的内容与他在 2:26 屏幕上显示的内容完全相同,这是我的代码:
import 'package:flutter/material.dart';
import 'dart:html' as html;
extension HoverExtension on Widget{
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,“扩展”、“开”和“小部件”都被红线了。当我将鼠标悬停在“扩展”上时,它会显示以下内容:
Undefined class 'extension'.
Try changing the name to the name of an existing class, or creating a class with the name 'extension'. dartundefined_class
This requires the 'extension-methods' language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 2.6 or higher, and running 'pub get'. dart(experiment_not_enabled) …Run Code Online (Sandbox Code Playgroud) 我有这样的东西,其中小部件的颜色取决于布尔值,当您点击小部件时布尔值会翻转。但我还想初始化 bool,对于小部件的不同实例,它会有所不同。我尝试了类似的方法,其中StatefulWidget类包含初始值initBool,该值用于initState设置myBool。
它似乎有效(但老实说,我不确定,因为我遇到了一个奇怪的错误,但我不知道它是否相关),但它只是感觉有点笨重,所以我想知道是否有更多在 Flutter 中执行此操作的适当方法。谢谢!
class MyBox extends StatefulWidget {
final bool initBool;
MyBox(this.initBool);
@override
_MyBoxState createState() => _MyBoxState();
}
class _MyBoxState extends State<MyBox> {
bool myBool;
@override
void initState() {
super.initState();
myBool = widget.initBool;
}
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
color: myBool ? Colors.green : Colors.red
),
onTap: () {
setState(() {
myBool = !myBool;
});
},
);
}
}
Run Code Online (Sandbox Code Playgroud) 截至目前,我们可以将 Flutter Web 应用程序作为单个文件启动,该文件将立即加载,因此需要花费大量时间和带宽来加载,这并不理想,有没有办法一次只加载一个页面,而不是整个网络应用程序。我的意思是,一次加载一个小部件。
任何建议将不胜感激。
我一直在尝试将从 Firebase 的 Cloud Firestore 检索到的文档转换为 Swift 5 中的自定义对象。我正在关注文档:
但是,Xcode 向我显示Value of type 'NSObject' has no member 'data'了该行的错误try $0.data(as: JStoreUser.self)。我已将结构定义为Codable.
编码:
func getJStoreUserFromDB() {
db = Firestore.firestore()
let user = Auth.auth().currentUser
db.collection("users").document((user?.email)!).getDocument() {
(document, error) in
let result = Result {
try document.flatMap {
try $0.data(as: JStoreUser.self)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用户结构:
public struct JStoreUser: Codable {
let fullName: String
let whatsApp: Bool
let phoneNumber: String
let email: String
let creationDate: Date?
} …Run Code Online (Sandbox Code Playgroud) ios firebase google-cloud-platform swift google-cloud-firestore