Снэк-бар не работает, флаттер NoSuchMethodError - PullRequest
0 голосов
/ 01 апреля 2020

Я новичок ie попытался создать Snackbar, создав глобальный ключ, вызов _scaffoldkey и добавив его в эшафот. Затем я попытался создать снэк-бар, используя этот ключ, но он не работает, я получил отзыв о том, что "NoSuchMethodError: метод 'showSnackBar' был вызван нулем. Receiver: 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 Ответ

0 голосов
/ 01 апреля 2020

В функции onTap GestureDetector вы забыли фигурные скобки в другом состоянии. Я добавил () => Попробуйте это:

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