小吃店不工作颤振NoSuchMethodError

Posted

技术标签:

【中文标题】小吃店不工作颤振NoSuchMethodError【英文标题】:Snack Bar not working flutter NoSuchMethodError 【发布时间】:2020-07-12 14:34:07 【问题描述】:

我是一个颤抖的新手,尝试通过创建全局键调用 _scaffoldkey 并将其附加到脚手架来创建 Snackbar。然后我尝试使用此键创建小吃店,但它不起作用,我收到反馈说“NoSuchMethodError:方法'showSnackBar'被调用为null。接收者:Null尝试调用:showSnackBar('SnackBar'的实例) 有人可以帮忙吗?

 final _scaffoldKey = GlobalKey<ScaffoldState>(); 
   _displaySnackBar(BuildContext context) 
    final snackBar = SnackBar(content: Text('Are you talkin\' to me?'));
    _scaffoldKey.currentState.showSnackBar(snackBar);  
  

    return Scaffold(
        key: _scaffoldKey,  
        body: SingleChildScrollView(
      child: Column(
        children: <Widget>[
          SafeArea(
            child: Container(
              width: width,
              child: Column(
                children: <Widget>[
                  Hero(
                    tag: 'h' + index.toString(),
                    child: Stack(
                      children: <Widget>[
                        Container(
                          height: height * (0.4),


                          child: CachedNetworkImage(
                            width: width,
                            height: height * (0.4),
                            fit: BoxFit.fill,
                            imageUrl: imageUrl,

                            placeholder: (context, url) => spinKit(),
                            errorWidget: (context, url, error) => Container(
                                constraints: BoxConstraints.expand(),
                                child: Container(
                                  alignment: Alignment.topLeft,
                                  color: Colors.blue,
                                  child: Center(child: Icon(Icons.error)),
                                )),
                          ),
                        ),
                        SafeArea(
                          child: Container(
                            alignment: Alignment.topLeft,
                            padding: const EdgeInsets.all(5),
                            child: Icon(
                              Icons.arrow_back_ios,
                              size: 40,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    alignment: Alignment.bottomCenter,
                    child:  Container(
      width: width,
      color: Colors.black,
      child: Row(
        children: <Widget>[
          GestureDetector(
            onTap: overFlowRight
                ? () 
                    Navigator.of(context)
                        .push(_createRoute2(_devotionNotifier, index + 1));
                  
                : _displaySnackBar(context),

            child: Container(
              padding: const EdgeInsets.only(left: 5),
              child: Icon(
                Icons.arrow_left,
                size: 50,
                color: Colors.white,
              ),
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.only(left: 0, right: 0),
              child: Text(
                _devotionNotifier.devotionList[index].topic,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 30.0,
                  fontFamily: 'Pacifico',
                  color: Colors.white,
                ),
              ),
            ),
          ),
          GestureDetector(
            onTap: overFlowLeft
                ? () 
                    Navigator.of(context)
                        .push(_createRoute(_devotionNotifier, index - 1));
                  
                : _displaySnackBar(context),

            child: Container(
              padding: const EdgeInsets.only(right: 5),
              child: Icon(
                Icons.arrow_right,
                size: 50,
                color: Colors.white,
              ),
            ),
          ),
        ],
      )
      ),
                  )
                ],
              ),
            ),
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.all(10),
                  child: Text(
                    'Memory Verse :' +
                        _devotionNotifier.devotionList[index].verse,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 15.0,
                      fontFamily: 'Merriweather',
                      color: Colors.grey,
                    ),
                  ),
                ),
              ),
              SizedBox(
                width: 25,
              ),
              Expanded(
                child: Container(
                  alignment: Alignment.centerRight,
                  padding: const EdgeInsets.all(10),
                  child: Text(
                    'Date :' + _devotionNotifier.devotionList[index].time,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 15.0,
                      fontFamily: 'Merriweather',
                      color: Colors.grey,
                    ),
                  ),
                ),
              ),
            ],
          ),
          Padding(
            padding:
                const EdgeInsets.only(left: 12, right: 12, bottom: 20, top: 12),
            child: Center(
              child: Text(
                _devotionNotifier.devotionList[index].content,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 20.0,
                  fontWeight: FontWeight.w600,
                  fontFamily: 'Bellota',
                  color: Colors.black,
                ),
              ),
            ),
          ),
        ],
      ),
    ));

【问题讨论】:

你真的需要粘贴那么多代码吗?我的意思是,为什么? 【参考方案1】:

在 GestureDetector 的 onTap 函数中,您在 else 条件下忘记了大括号。我添加了 ()=> 试试这个:

 final _scaffoldKey = GlobalKey<ScaffoldState>(); 
   _displaySnackBar(BuildContext context) 
    final snackBar = SnackBar(content: Text('Are you talkin\' to me?'));
    _scaffoldKey.currentState.showSnackBar(snackBar);  
  

return Scaffold(
    key: _scaffoldKey,  
    body: SingleChildScrollView(
  child: Column(
    children: <Widget>[
      SafeArea(
        child: Container(
          width: width,
          child: Column(
            children: <Widget>[
              Hero(
                tag: 'h' + index.toString(),
                child: Stack(
                  children: <Widget>[
                    Container(
                      height: height * (0.4),


                      child: CachedNetworkImage(
                        width: width,
                        height: height * (0.4),
                        fit: BoxFit.fill,
                        imageUrl: imageUrl,

                        placeholder: (context, url) => spinKit(),
                        errorWidget: (context, url, error) => Container(
                            constraints: BoxConstraints.expand(),
                            child: Container(
                              alignment: Alignment.topLeft,
                              color: Colors.blue,
                              child: Center(child: Icon(Icons.error)),
                            )),
                      ),
                    ),
                    SafeArea(
                      child: Container(
                        alignment: Alignment.topLeft,
                        padding: const EdgeInsets.all(5),
                        child: Icon(
                          Icons.arrow_back_ios,
                          size: 40,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                alignment: Alignment.bottomCenter,
                child:  Container(
  width: width,
  color: Colors.black,
  child: Row(
    children: <Widget>[
      GestureDetector(
        onTap: overFlowRight
            ? () 
                Navigator.of(context)
                    .push(_createRoute2(_devotionNotifier, index + 1));
              
            : ()=> _displaySnackBar(context),

        child: Container(
          padding: const EdgeInsets.only(left: 5),
          child: Icon(
            Icons.arrow_left,
            size: 50,
            color: Colors.white,
          ),
        ),
      ),
      Expanded(
        child: Padding(
          padding: EdgeInsets.only(left: 0, right: 0),
          child: Text(
            _devotionNotifier.devotionList[index].topic,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 30.0,
              fontFamily: 'Pacifico',
              color: Colors.white,
            ),
          ),
        ),
      ),
      GestureDetector(
        onTap: overFlowLeft
            ? () 
                Navigator.of(context)
                    .push(_createRoute(_devotionNotifier, index - 1));
              
            : ()=> _displaySnackBar(context),

        child: Container(
          padding: const EdgeInsets.only(right: 5),
          child: Icon(
            Icons.arrow_right,
            size: 50,
            color: Colors.white,
          ),
        ),
      ),
    ],
  )
  ),
              )
            ],
          ),
        ),
      ),
      Row(
        children: <Widget>[
          Expanded(
            child: Container(
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.all(10),
              child: Text(
                'Memory Verse :' +
                    _devotionNotifier.devotionList[index].verse,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 15.0,
                  fontFamily: 'Merriweather',
                  color: Colors.grey,
                ),
              ),
            ),
          ),
          SizedBox(
            width: 25,
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              padding: const EdgeInsets.all(10),
              child: Text(
                'Date :' + _devotionNotifier.devotionList[index].time,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 15.0,
                  fontFamily: 'Merriweather',
                  color: Colors.grey,
                ),
              ),
            ),
          ),
        ],
      ),
      Padding(
        padding:
            const EdgeInsets.only(left: 12, right: 12, bottom: 20, top: 12),
        child: Center(
          child: Text(
            _devotionNotifier.devotionList[index].content,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 20.0,
              fontWeight: FontWeight.w600,
              fontFamily: 'Bellota',
              color: Colors.black,
            ),
          ),
        ),
      ),
    ],
  ),
));

【讨论】:

以上是关于小吃店不工作颤振NoSuchMethodError的主要内容,如果未能解决你的问题,请参考以下文章

NoSuchMethodError - 颤振[重复]

颤振和json:NoSuchMethodError:方法'[]'在null上被调用

怎么解决?颤振(6629):[错误:颤振/lib/ui/ui_dart_state.cc(209)]未处理的异常:NoSuchMethodError

“未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'iterator'。”当我在颤振中使用 google_maps_polyline 插件时抛出

颤振在底部表顶部显示 Snackbar

Flutter Redux 小吃店