1

I am new to dart. I want to do something like this. But editor says it is 'unnecessary_null_comparison'. What is the best way in dart to do something like this?

 EventType? et = dbHandler.getEventType(widget.eventName) as EventType;
 if (et != null) {
   dbHandler.insertEventType(eventType);
 }

2 Answers 2

2

If dbHandler.getEventType(widget.eventName) returns a nullable value you should use as EventType?. Then your code should work as expected.

EventType? et = dbHandler.getEventType(widget.eventName) as EventType?;
if (et != null) {
  dbHandler.insertEventType(et);
}
Sign up to request clarification or add additional context in comments.

1 Comment

So, you just check if it is null like any other language, by comparing with the null keyword? Can no null safety signs help here to make it a bit shorthand?
2

Dart has sound null safety so when you casted it to EventType using as EventType, it is not nullable by definition, that's why it's saying the null comparison is unnecessary because it'll always evaluate to true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.