Dart:具有命名参数并执行函数的构造函数
我想创建一个带有命名参数的类,它也执行一个函数。但是,我无法找到从构造函数调用函数的解决方案。是否有类似默认 init 函数的东西?
我的主要目标是在创建类后立即触发 init 函数,以显示 toast 通知。有谁知道我如何存档?
我正在使用的包: https://pub.dev/packages/fluttertoast
这是我的代码:
class CustomToast {
CustomToast({required this.message});
final String message;
late FToast fToast;
void init() {
fToast = FToast();
fToast.init(globalKey.currentState!.context);
_showToast();
}
_showToast() {
fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 1),
);
}
Widget toast = Container(...);
}
亲切的问候
I would like to create a class with named parameters which also executes a function. However, I am not able to find a soluiton to call a function from the constructor. Is there something like a default init function?
My main goal is to trigger the init
function as soon as the class is being createtd in order to show a toast notification. Does anyone knows how I can archive that?
The package I am using: https://pub.dev/packages/fluttertoast
This is my code:
class CustomToast {
CustomToast({required this.message});
final String message;
late FToast fToast;
void init() {
fToast = FToast();
fToast.init(globalKey.currentState!.context);
_showToast();
}
_showToast() {
fToast.showToast(
child: toast,
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 1),
);
}
Widget toast = Container(...);
}
Kind regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能为你的构造函数添加一个主体吗?
这会将以下内容打印到控制台:
Could you add a body to your constructor?
This prints the following to the console: