1

I want to Open Drawer in flutter and used Scaffold key and was working before but after the flutter upgrade, I am getting this error:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.

My code goes like this:

I declared a Global Scaffold key GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey(); then called it by flat button

FlatButton.icon(
                     label: Text("filter",style: style2.copyWith(color:Colors.lightGreenAccent[400],fontWeight: FontWeight.bold),),
                     onPressed: (){
                      _scaffoldKey.currentState.openEndDrawer();
                     },
),

enter image description here

2 Answers 2

4

An expression whose value can be 'null' must be null-checked before it can be dereferenced. Try checking that the value isn't 'null' before dereferencing it.

=> The statement is saying when using a nullable variable or expression, be sure to handle null values. For example, you can use an if statement, the ?? operator, or the ?. operator to handle possible null values.

Refer the official documentation for more details: https://dart.dev/null-safety#using-variables-and-expressions

And so you should use ?. operator for handling the possible null value of the currentState:

_scaffoldKey.currentState?.openEndDrawer();
Sign up to request clarification or add additional context in comments.

Comments

2

You can change your method like this:

_scaffoldKey.currentState?.openEndDrawer();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.