Home > Enterprise >  Error Message: A nullable expression can't be used as a condition
Error Message: A nullable expression can't be used as a condition

Time:03-24

How to fix this. Error message A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition. enter image description here

This is my code.

floatingActionButton: FloatingActionButton(
      onPressed: () => _webcamVideoElement.srcObject.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),
      tooltip: 'Start stream, stop stream',
      child: Icon(Icons.camera_alt),
    ),
  );
}

CodePudding user response:

Try this:

onPressed: () => _webcamVideoElement?.srcObject?.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),

CodePudding user response:

It's pretty simple, before each property that can be null you should check null, try to click "Quick Fix" with Ctrl . (Mac Command .) to see what is suggested and that will probably fix it.

Also you didn't showed all your code, so I only can guess you are dealing with (VideoElement)[https://api.flutter.dev/flutter/dart-html/VideoElement-class.html] in that case your srcObject can (be null)[https://api.flutter.dev/flutter/dart-html/MediaElement/srcObject.html] in this case pretty close to what @Mohammad said, I suggest you to try this:

floatingActionButton: FloatingActionButton(
      onPressed: () => _webcamVideoElement.srcObject?.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),
      tooltip: 'Start stream, stop stream',
      child: Icon(Icons.camera_alt),
    ),
  );
}
  • Related