Hello friends, Recently when I was working on a tutorial for my website, Then I thought let’s use our previous written code so we have to code less. So I simply copy and paste the previous code then I saw It is throwing me a large error known as ” The parameter ‘key’ can’t have a value of ‘null’ because of its type, but the implicit default value is ‘null’. Try adding either an explicit non-‘null’ default value or the ‘required’ modifier “. So I started Google it and here you go, I found a fix solution. This error came because of NULL SAFETY feature of Dart. So in this tutorial we would learn about Flutter Solve The parameter ‘key’ can’t have a value of ‘null’ because of its type.
Contents in this project Flutter Solve The parameter ‘key’ can’t have a value of ‘null’ because of its type :-
1. We would solve this error, But we have to understand it first. So below is the line of code before solving the error.
1 |
ScreenName({Key key, @required this.variable}) : super(key: key); |
Above code is mostly used when we want to receive value from previous activity screen using Key.
2. Now to solve this error all we have to do is, We have to make the Key as Non Null default value acceptable using ? (Question Mark) operator.
1 |
ScreenName({Key? key, @required this.variable}) : super(key: key); |
3. Now mostly it will solve our error but what I have seen in few cases we have to remove the @ sign from the required keyword. Sometimes you need it or sometimes you don’t.
Example 1 :
1 |
ScreenName({Key? key, @required this.variable}) : super(key: key); |
Example 2 :
1 |
ScreenName({Key? key, required this.variable}) : super(key: key); |
Try example 1 or example 2. One of them will surely work. Thanks for Reading 🙂 .