4 ms·
It's a non-null assertion operator which would throw an error if `e1 == null`.
by mraleph 7y ago
It's a non-null assertion operator which would throw an error if `e1 == null`.
- williamdclt 7y agoWould the operator itself throw the error, or trying to access `[e2]` on null? I never used Dart, I would assume it's basically a cast from `Optional<SomeType>` to `SomeType`, so wouldn't throw an exception itself
- mraleph 7y agoThe operator itself would throw an error. Dart is planning to have a sound non-nullability which means that if something has static type `SomeType` then it is guaranteed to be a value that is actually `SomeType` and not something else. Thus you can't take `SomeType?` and just cast it to `SomeType` because `SomeType?` can be null and `SomeType` can't be null. This means this cast has to perform a check and throw if your value is null. This also means that if you have `e1` and `e1` has non-nullable static type then `e1` would never be null in the NNBD world - which is a very good property (e.g. for optimizations).
- williamdclt 7y agoInteresting! So you were right to call it an assertion rather than a cast