requestCameraPermissionWeb method
Implementation
Future<String> requestCameraPermissionWeb() async {
try {
// Check if running on HTTPS (required for camera permissions)
final protocol = web.window.location.protocol;
final hostname = web.window.location.hostname;
debugPrint(
'Camera permission check - Protocol: $protocol, Hostname: $hostname',
);
if (!protocol.startsWith('https') && hostname != 'localhost') {
debugPrint(
'Camera permission requires HTTPS context - Current: $protocol://$hostname',
);
return 'denied';
}
final mediaDevices = web.window.navigator.mediaDevices;
debugPrint('Media devices: $mediaDevices');
final constraints = web.MediaStreamConstraints(
video: true.toJS,
audio: false.toJS,
);
final stream = await mediaDevices.getUserMedia(constraints).toDart;
// Stop the stream immediately as we only wanted to check permission
final tracks = stream.getTracks();
for (int i = 0; i < tracks.length; i++) {
tracks[i].stop();
}
return 'granted';
} catch (e) {
debugPrint('Camera permission error: $e');
if (e is web.DOMException) {
final errorName = e.name;
if (errorName == 'NotAllowedError') {
return 'denied';
} else if (errorName == 'NotFoundError') {
return 'unsupported';
} else if (errorName == 'PermissionDeniedError') {
return 'denied';
}
}
return 'error';
}
}