dart_eval icon indicating copy to clipboard operation
dart_eval copied to clipboard

value is null, but 'value == null' is false

Open moshstudio opened this issue 2 years ago • 1 comments

Map e = ...;
for (var item in e) {
    print(item['displayLogId']); // null
    bool ifNull = item['displayLogId'] == null;
    print(ifNull); / false
}

i print item['displayLogId'] which is null, but for item['displayLogId'] == null, it is false. Is it right, or there is a better method to use?

moshstudio avatar Nov 16 '23 03:11 moshstudio

Hello @cuifengcn,

I'm Michael and I'm interested in contributing to this project!

To answer this question, I have had a similar issue with Map and a trick was to use containsKey. See here for reference.

A correction to your code will be like:

bool containsKey = item.containsKey('displayLogId'); // check if the key 'displayLogId' exists
bool isNull = containsKey && item['displayLogId'] == null; // you can now check if the value for `displayLogId` is `null`
print(isNull);

Let me know if you have any questions about this solution.

DeleMike avatar Nov 18 '23 12:11 DeleMike