小吃店不工作颤振 NoSuchMethodError

Snack Bar not working flutter NoSuchMethodError

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

 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,
                ),
              ),
            ),
          ),
        ],
      ),
    ));

在 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,
            ),
          ),
        ),
      ),
    ],
  ),
));