Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }

>Solution :

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);}
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading