What would be the best way to check if a pointer is a nullpointer in Dart using FFI?
Current approach is this:
ffi.Pointer<Thing> thing = ... // can potentially return null
if(thing == ffi.nullptr) {
...
But, of course, this gives a "Equality operator ==
invocation with references of unrelated types." warning message.
The API is still quite new, and I am not exactly sure how to approach this. I could also be overthinking this and there's a super simple answer.
Well, I have yet to try dart:ffi but from the API documentation for == you can see:
Equality for Pointers only depends on their address
https://api.dart.dev/dev/2.6.0-dev.0.0/dart-ffi/Pointer-class.html
So, what you can do is just to compare the address of the two pointers:
ffi.Pointer<Thing> thing = ... // can potentially return null
if(thing.address == ffi.nullptr.address) {
...
In fact, a nullpointer are just a Pointer which points to the address 0:
final Pointer<Void> nullptr = Pointer.fromAddress(0)
https://api.dart.dev/dev/2.6.0-dev.0.0/dart-ffi/nullptr.html