当我在发送数据时使用showSnackBar时,我遇到了这个问题(颤动未处理的异常: NoSuchMethodError:在null上调用了getter 'currentState‘)。它出现在数据发送之后,我不知道我到底在哪里做错了
class ProductNotifyDialog {
BuildContext context;
Product product;
GlobalKey<ScaffoldState> scaffoldKey;
ProductNotifyDialog({
this.context,
this.product,
}) {
showDialog(
context: context,
builder: (context) {
return SimpleDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(15.0))),
// contentPadding: EdgeInsets.symmetric(horizontal: 20),
titlePadding: EdgeInsets.fromLTRB(0, 15, 16, 15),
title: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(25.0),
child: Container(
width: MediaQuery.of(context).size.width,
child: Text(
S
.of(context)
.do_you_want_us_to_notify_when_it_is_in_stock_again,
style: Theme.of(context).textTheme.headline4,
textAlign: TextAlign.center,
),
),
),
],
),
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 120,
height: 45,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
color: Colors.grey,
onPressed: () {
Navigator.pop(context);
},
child: Text(
S.of(context).no,
style: TextStyle(color: Colors.white),
),
),
),
SizedBox(width: 25),
Container(
width: 120,
height: 45,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
color: Theme.of(context).accentColor,
onPressed: () {
_submit();
//_con.notifyAboutProduct(widget.product);
Navigator.pop(context);
},
child: Text(
S.of(context).yes,
style: TextStyle(color: Colors.white),
),
),
),
],
mainAxisAlignment: MainAxisAlignment.center,
),
SizedBox(height: 10),
],
);
});
}
void _submit() {
repo.notifyUnavailableProduct(product).then((value) {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(S.current.your_request_has_been_submitted),
));
});
Navigator.pop(context);
}}
我试图在发送数据后在snackbar中显示消息,但遇到此错误,有人能帮助我吗?
发布于 2020-06-09 12:53:50
scaffoldKey必须包含该值,并且必须在Scaffold key属性中输入scaffoldKey。
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
https://stackoverflow.com/questions/62274577
复制相似问题