close
The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided, The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided, The parameter 'key' can't have a value of 'null' because of its type and no non-null, type and no non-null default

[Solved] The parameter ‘key’ can’t have a value of ‘null’ because of its type, and no non-null default value is provided

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. The parameter ‘key’ can’t have a value of ‘null’ because of its type, and no non-null default value is provided in the flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How The parameter ‘key’ can’t have a value of ‘null’ because of its type, and no non-null default value is provided Error Occurs ?

I have stateless widget as like below.

class ProductScreen extends StatelessWidget {
  const ProductScreen({Key key}): super(key: key);

  // ...
}

When I run my app I get the following error:

The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided.

How to Solve The parameter ‘key’ can’t have a value of ‘null’ because of its type, and no non-null default value is provided Error?

Question:
Answer:

  1. How to Solve The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided Error?

    To solve The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided error Just use the key as an optional parameter. just like below. I just Add the Key? key in the below code. That means if you are passing a parameter key then this class will take that parameter.

  2. The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided

    To solve The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided error Just use the key as an optional parameter. just like below. I just Add the Key? key in the below code. That means if you are passing a parameter key then this class will take that parameter.

Solution 1: Make nullable Parameter

Problem is when you define your StatelessWidget there you are not passing params Key that’s why you are facing this issue.

Make nullable Parameter

Just use key as optional parameter. just like below. I just Add Key? key in below code. That mean if you are passing parameter key then this class will take that parameter.

class ProductScreen StatelessWidget {
  const ProductScreen({Key? key}): super(key: key);

  // ...
}

Solution 2: Make Parameter required

Just add required Key key. So that whenever you want to define ProductScreen class you have to pass key parameter as required field.

class ProductScreen StatelessWidget {
  const ProductScreen({required Key key}): super(key: key);

  // ...
}

Solution 3: Remove Line

If you are sure you don’t want to pass any parameter to your class then just remove that line.

class ProductScreen StatelessWidget {
  // ...
}

Summery

So, It’s All About this Error. I hope this tutorial helps you to solve your error. Please Comment Below if You stucks anywhere with my code.

Also Check Out Below Tutorials


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *