void show() {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(
title,
style: TextStyle(fontSize: 16),
),
backgroundColor: haserror ? errorColor : succses,
behavior: isfloating ? SnackBarBehavior.floating : SnackBarBehavior.fixed,
margin: isfloating ? EdgeInsets.all(20) : null,
duration: Duration(seconds: 2),
action: actionTile != ""
? SnackBarAction(
label: actionTile,
onPressed: onPressed == null
? () {
scaffoldKey.currentState.hideCurrentSnackBar();
}
我正在尝试运行一些项目,但是有这个错误。
"message": "The method 'showSnackBar' isn't defined for the type 'ScaffoldState'.\nTry correcting the name to the name of an existing method, or defining a method named 'showSnackBar'.",
}]
我仍然困惑于如何解决,在颤振发展中仍然是新的。
发布于 2022-11-21 15:47:50
在较新版本的颤振中,您必须使用ScaffoldMessenger
来显示快捷键。因此,您不需要存储scaffoldKey
。
如果您使用的是StatefulWidget
,请尝试如下:
void show() {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(...),
);
}
如果使用的是StatelessWidget
,则需要在调用时将context
传递给show
方法。在这种情况下试试这个:
void show(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(...),
);
}
因为隐藏了零食条:
ScaffoldMessenger.of(context).hideCurrentSnackBar();
https://stackoverflow.com/questions/74520936
复制相似问题