Home > Enterprise >  The argument type 'BuildContext?' can't be assigned to the parameter type 'Build
The argument type 'BuildContext?' can't be assigned to the parameter type 'Build

Time:01-02

I am making a quiz app and I want to assign a key to each of the questions so I can automatically scroll to the next one. For some reason I get this error.

The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext'

This is where I get this error:

Code:

final GlobalKey key;

void scrollPage() {
      Scrollable.ensureVisible(widget.key.currentContext //THIS IS WHERE I GET THE ERROR,
          alignment: 1, duration: Duration(seconds: 2), curve: Curves.ease);

      print(widget.offset);
    }

CodePudding user response:

It is possible to have null context. you can add ! after checking null.

void scrollPage() {
   if(widget.key.currentContext!=null){
      Scrollable.ensureVisible(widget.key.currentContext!,
          alignment: 1, duration: Duration(seconds: 2), curve: Curves.ease);

      print(widget.offset);}
    }
  • Related