简体   繁体   English

Snackbar 和异常:scaffold.of()

[英]Snackbar and exception: scaffold.of()

Why after tap on my iconButton my snackBar throw me exception with Scaffold.of() ?为什么点击我的iconButton后,我的snackBar抛出Scaffold.of()异常?

Scaffold.of() called with a context that does not contain a Scaffold.
No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.
There are several ways to avoid this problem. The simplest is to use a Builder to get a context that is "under" the Scaffold. For an example of this, please see the documentation for Scaffold.of():
  https://api.flutter.dev/flutter/material/Scaffold/of.html
A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold. In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of().
A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
The context used was:
  Builder

my code:我的代码:

        : Scaffold(
          backgroundColor: Colors.white,
          body: Container(
            child: ListView.builder(
              itemCount: user.getInvolvedEvents.length,
              itemBuilder: (context, i){
                    return GestureDetector(
                        onTap: () {
                          //
                        },
                        child: Container(
                          height: 160.0,
                          margin: EdgeInsets.all(5),
                          child: Card(
                            child: Container(
                              decoration: BoxDecoration(
                                child: Stack(
                                  alignment: Alignment.bottomLeft,
                                  children: <Widget>[
                                    Positioned(
                                      bottom: 15,
                                      right: 20,
                                      child: Row(
                                        children: <Widget>[
                                          IconButton(
                                            icon: Icon(Icons.cancel, color: Colors.red, size: 35),
                                            onPressed: () {
                                              return showDialog(
                                                context: context,
                                                builder: (BuildContext context){
                                                  return AlertDialog(
                                                    content: Text('Czy napewno chcesz odwołać swój udział w wydarzeniu "${user.getInvolvedEvents[i].title}"?'),
                                                    actions: <Widget>[
                                                    FlatButton(
                                                      child: Text(
                                                        'TAK',
                                                        style: TextStyle(
                                                            fontWeight: FontWeight.bold,
                                                            color: Colors.black),
                                                      ),
                                                      onPressed: () async {
                                                        bool status = await removeFromInterested(user.getInvolvedEvents[i].id);
                                                        if(status){
                                                          Navigator.of(context).pop(null);

                                                          final snackBar = SnackBar(
                                                            duration: const Duration(seconds: 3),
                                                            content: Text('Nie weźmiesz udziału w wydarzeniu'),
                                                          );

                                                          Scaffold.of(context).showSnackBar(snackBar);
                                                        }
                                                      },
                                                    ),

thanks for any help:)谢谢你的帮助:)

This is because your are using context of Alert that doesn't contain scaffold .这是因为您正在使用不包含scaffoldAlert context

You can use GlobalKey instead:您可以改用GlobalKey

  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
    )
  }

and show snackbar:并显示小吃吧:

_scaffoldKey.currentState.showSnackBar(
  SnackBar(
    content: Text('Text'),
  )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of() - Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时 - Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold Scaffold.of会找到遥远的窗口小部件吗? - Will Scaffold.of ever find distant widget anestors? 错误:没有名为“nullOk”的命名参数 Scaffold.of(context, nullOk: true) - Error: No named parameter with the name 'nullOk' Scaffold.of(context, nullOk: true) Flutter Scaffold.of(context).openDrawer() 不起作用 - Flutter Scaffold.of(context).openDrawer() doesn't work 如何在没有脚手架的情况下显示snackBar - How to show snackBar without Scaffold 在动态脚手架中显示 SnackBar 失败 - Show a SnackBar in a dynamic Scaffold fails 全局脚手架在颤动中显示 Snackbar - Global scaffold to show Snackbar in flutter 处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用 - The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM