Skip to content
tezvyn:

Describe Dart's null-aware ?. and null assertion ! operators

Source: dart.devMediumHow cards are made

Describe Dart's null-aware ?. and null assertion ! operators

Tests Dart null safety mechanics: ?. short-circuits member access on null, while ! casts away nullability. A strong answer notes ?. avoids NPEs and ! is a risky opt-out. Red flag: claiming ! is safe or that ?. provides a default value.

What's really being asked

This question probes whether you understand the mechanics of Dart sound null safety, specifically the difference between safely handling a potential null via short-circuiting and forcibly overriding the type system. A senior candidate should demonstrate that ?. is a defensive tool for nullable chains and ! is an escape hatch that shifts safety guarantees from compile time to the developer's judgment.

The full answer

First, define the null-aware operator ?. as an access modifier that evaluates to null immediately if the receiver is null, otherwise dereferences the member. Second, define the null assertion operator ! as a postfix cast that converts a nullable expression to its non-nullable base type, throwing a runtime exception if the value is actually null. Third, give a correct ?. scenario such as reading a property on an optional model object where a null result is acceptable and should propagate. Fourth, give a correct ! scenario such as consuming data from an untyped source like parsed JSON where you have external knowledge the value is present and you want to assign it to a non-nullable variable.

The mistakes people make

A major red flag is conflating ?. with ??; the former propagates null through a chain while the latter supplies a default value. Another mistake is sprinkling ! everywhere as a quick fix for compile errors rather than fixing underlying nullability design. Candidates also err by claiming ! is safe because they personally checked the value, without acknowledging that the compiler cannot verify this and that runtime crashes will occur if the assumption ever breaks. Using ?. in a context that demands a non-nullable result without a fallback is also incorrect.

What usually comes next

The interviewer may ask how Dart's flow analysis and type promotion reduce the need for the ! operator. They might ask you to compare ?? and ?. in a chained expression. They could probe how you would remove unnecessary ! operators from a legacy codebase migrating to null safety. A senior interviewer might also ask about the performance or codegen implications of these operators, or how they interact with late variables.

A concrete example

For the null-aware operator, consider a variable declared as String? maybeName that may hold a name. To safely get its length, you write int? maybeLength = maybeName?.length. If maybeName is null, the expression stops and maybeLength is assigned null; otherwise it receives the string length. For the null assertion operator, imagine a JSON map parsed as Map<String, dynamic?> where a specific key is guaranteed by your backend contract to hold a non-null string. You write String title = json['title']!. This tells the compiler to treat the result as non-nullable String, and if the guarantee is violated the program throws a runtime error, which is appropriate when a null would indicate a contract breach rather than a normal edge case.

Interview question

When accessing a property on a nullable object in Dart, what is the fundamental difference between using ?. and !?

  • a.Both operators avoid runtime errors, but ?. is preferred for chained property access.
  • b.! returns null on a null receiver, whereas ?. throws a runtime null assertion error.
  • c.?. evaluates to null when the receiver is null, while ! casts to non-nullable and may throw at runtime.Correct
  • d.?. provides a default value if the object is null, while ! enforces compile-time null checks.
Why?

The null-aware operator ?. short-circuits and yields null if the receiver is null, whereas ! forcibly casts away nullability and throws a runtime exception if the value is actually null. Option D is wrong because it confuses ?. with the ?? default-value operator and incorrectly suggests ! is compile-time safe.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles