skip to Main Content

I’m getting weird error,

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

I think assigning ‘UserData?’ to ‘UserData’ is unsafe but, its obvious that I have definitely defined value. How can I add undefinition to make my user data to be accepted by the dart?

2

Answers


  1. Use the null assertion operator ! when you’re sure that the value is not null:

    UserData userData = userDataOrNull!;
    
    Login or Signup to reply.
  2. Just pass null assertion operator ! with object:

    void myFunction(UserData userData) {
      // use object here
    }
    
    
    myFunction(userData!); // Using ! to assert it's non-null
    

    Make sure to check the object is not null while adding null assertion operator.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search