Search code examples
user-interfaceflutterdartscaffoldsnackbar

Snack Bar not working flutter NoSuchMethodError


I am a flutter newbie tried creating a Snackbar by creating Global key call _scaffoldkey and appending it to the scaffold. I then tried tried creating a snack bar using this key but it isn't working, I got a feedback saying "NoSuchMethodError : The method 'showSnackBar' was called on null. Receiver : Null Tries calling : showSnackBar (Instance of 'SnackBar') Can someone please help?

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

Solution

  • In onTap function of GestureDetector you forgot braces in else condition.I added ()=> Try this:

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