flutter - The function can't be unconditionally invoked because it can be 'null'. The '!' will have no effect because the receiver can't be null - TagMerge
1The function can't be unconditionally invoked because it can be 'null'. The '!' will have no effect because the receiver can't be nullThe function can't be unconditionally invoked because it can be 'null'. The '!' will have no effect because the receiver can't be null

The function can't be unconditionally invoked because it can be 'null'. The '!' will have no effect because the receiver can't be null

Asked 1 years ago
2
1 answers

There are few ways I suggest. You can use any one of them

1.You can change it like

if(widget.toggleView != null){
    widget.toggleView();
} 

2.add require in constructor to make toggleView cannot be null

Register({require this.toggleView});

3.if you don't want to change code and guarantee toggleView will never be null. Try this

onPressed: widget.toggleView!

One more thing change is

///final Function? toggleView; should not be accepted for onPressed
///you can use VoidCallback or Function()
final VoidCallback? toggleView;
/// final Function()? toggleView;

Use VoidCallback or Function() instead of Function for the onPressed, and it should work

Source: link

Recent Questions on flutter

    Programming Languages