Blazor best practice for Non-nullable warnings
Asked Answered
P

4

6

With blazor I get Non-nullable warnings all around the code. Those warnings seems to be wrong, however solving them introduces a lot of code with the only purpose to hide the warning while the value will never be null.

What is the best practice to solve or hide those warnings?

Example:

[Inject] private IStringLocalizer<Element> L { get; set; }
Element.razor.cs(5, 50): [CS8618] Non-nullable property 'L' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Poland answered 7/7, 2022 at 12:37 Comment(0)
X
12

As Blazor will always assign the property to a non-null value before executing your code, it is safe to disable the warning using = default!.

[Inject] private IStringLocalizer<Element> L { get; set; } = default!;
Xavier answered 7/7, 2022 at 12:49 Comment(1)
This didn't work for me. When I set the non-nullable to default I got a "cannot convert literal to non-nullable value" error.Tzong
G
4

I had similar problems when I first moved over to Nullable.

The Inject issue can be resolved like this:

[Inject] private IStringLocalizer<Element> L { get; set; } = default!;

You can use the null forgiving ! operator where you know the object won't be null, or the null coalescing operator to check for null and return a default value.

private string? value;
private bool hasValue => value is not null;

string SomeMethod()
{
  return hasValue
    ? value!
    : string.empty;
}

//or 

string SomeOtherMethod()
{
  return value ?? string.empty;
}
Guido answered 7/7, 2022 at 12:57 Comment(0)
S
1

You can also use the [NotNull] annotation from System.Diagnostics.CodeAnalysis. Not sure, when it was added though:

[Inject]
[NotNull]
protected IStringLocalizer<Element>? Localizer { get; set; }

This is how we use it. Problem: This still only works on properties, not on fields.

Short answered 22/2 at 9:36 Comment(6)
With [NotNull] I do still get the CS8618 Non-nullable warning. But you have an ? as it is a nulalble value.Poland
@Poland I don't get these warnings (And they're not in e.g. <NoWarn> in the projects)... Having ? together with [NotNull] is ok as it works for us. Do you use this on properties with { get; set; } or on fields? It won't work with fields...Short
With { get; set; }Poland
Strange... I use this in several projects like this (And even <TreatWarningsAsErrors>true</TreatWarningsAsErrors> is set in the project, there is no error)...Short
It is the combination? with [NotNull] that does the trick... It is a bit counterintuitive but works. Thanks.Poland
@Poland That's the thing, yes. Still strange, I agree.Short
C
0

We are using the required modifier. This has the advantage that the object is consistent if it is instantiated according to C# guidelines.

[Inject] public required IAnyService AnyService { get; init; };

With Dotnet 9 Blazor will ship the injection by ctor.

Consonant answered 21/8 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.