首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Snackbar和exception: scaffold.of()

Snackbar和exception: scaffold.of()
EN

Stack Overflow用户
提问于 2020-04-25 03:59:20
回答 1查看 2.2K关注 0票数 1

为什么在点击我的iconButton之后,我的snackBar抛出了Scaffold.of()异常

代码语言:javascript
复制
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

我的代码:

代码语言:javascript
复制
        : 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);
                                                        }
                                                      },
                                                    ),

感谢您的帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-25 04:05:56

这是因为您使用的是不包含scaffoldAlertcontext

您可以改用GlobalKey

代码语言:javascript
复制
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

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

和show snackbar:

代码语言:javascript
复制
_scaffoldKey.currentState.showSnackBar(
  SnackBar(
    content: Text('Text'),
  )
);
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61416520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档