gpt4 book ai didi

flutter - Flutter通过回调传递字符串

转载 作者:行者123 更新时间:2023-12-03 04:00:49 27 4
gpt4 key购买 nike

这是简单的代码,用于显示具有叠加支持的小部件,并且在onReplay中,我想获取字符串作为消息来显示,例如使用toast

showOverlayNotification((context) {
return MessageNotification(
message: messages[3],
onReplay: (message) {
OverlaySupportEntry.of(context).dismiss();
toast(message);
},
);
}, duration: Duration(milliseconds: 4000));

我实现的将消息作为字符串传递的代码不正确,我得到了

error: The final variable 'onReply' must be initialized.



我该如何解决这个问题?
typedef StringToVoidFunc = void Function(String);
const messages = [
...
];

class MessageNotification extends StatelessWidget {
//final VoidCallback onReplay;
final StringToVoidFunc onReply;
final String message;

const MessageNotification({
Key key,
this.onReplay,
@required this.message,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return SafeArea(
child: InkWell(
onTap: (){
if (onReplay != null) onReplay("sample pass message");
},
child: Center(
child: Text(
'ssss',
style: TextStyle(
fontSize: 26.0,
),
),
),
),
);
}
}

最佳答案

您在类型错误中做错了

改变这个

final StringToVoidFunc onReply;


final StringToVoidFunc onReplay;

错误已解决。

关于flutter - Flutter通过回调传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56969613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com