C
C#•9mo ago
Timo Martinson

Warning: NULL literal or a possible NULL value is being converted to a non-nullable type.

Ladies and gentlemen, I am confronted with a new type of warning, that I would like to understand. What should I do? This is the code:
[HttpGet("/{userSlug}")]
public async Task<User> ShowUser(string userSlug)
{
User user = await _dataContext.Users.FirstOrDefaultAsync((u) => u.Slug == userSlug);

return user;
}
[HttpGet("/{userSlug}")]
public async Task<User> ShowUser(string userSlug)
{
User user = await _dataContext.Users.FirstOrDefaultAsync((u) => u.Slug == userSlug);

return user;
}
This is the message: NULL literal or a possible NULL value is being converted to a non-nullable type
3 Replies
Timo Martinson
Timo MartinsonOP•9mo ago
what should I do ?? 😦
Pobiega
Pobiega•9mo ago
FirstOrDefaultAsync will return the first item matching the predicate, or default if nothing matches default for a reference type is null that means you are assingning null to User user, which is declared as non-nullable you must either change this to use First so it never returns null, or change the return value to Task<User?> if its intended that null is an acceptable return value
Timo Martinson
Timo MartinsonOP•9mo ago
Thank you. again!
Want results from more Discord servers?
Add your server