Reputation: 815
Im making a web-app that needs to enable torch/flash using Flutter. However, whenever I try to enable flash, I get an error:
Type Error: true: type 'bool' is not a subtype of type 'JSArray<Object?>?'
I've tested this on both Samsung Galaxy S21 and Google Pixel 6 Pro with Google Chrome for Android. Same error on both devices.
The preview works fine, and I've also tested recording, which works, but not setFlashMode(FlashMode.always)
, which throws the same error.
Here is my code:
import 'package:flutter/material.dart';
class CameraScreen extends StatefulWidget {
const CameraScreen({super.key});
@override
State<CameraScreen> createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
late CameraController controller;
bool isInitialised = false;
Future<void> initialiseCamera() async {
final cameras = await availableCameras();
controller = CameraController(cameras.last, ResolutionPreset.max, enableAudio: false);
if (!mounted) return;
try {
await controller.initialize();
setState(() {
isInitialised = true;
});
} catch (e) {
if (e is CameraException) {
switch (e.code) {
case 'CameraAccessDenied':
break;
default:
break;
}
}
}
}
void enableFlash() {
if (isInitialised) {
controller.setFlashMode(FlashMode.always);
}
}
@override
void initState() {
initialiseCamera();
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Align(
alignment: Alignment.center,
child: Builder(builder: (context) {
if (!isInitialised) {
return const CircularProgressIndicator();
}
return AspectRatio(
aspectRatio: 9 / 16, child: CameraPreview(controller));
}),
),
Align(
alignment: Alignment.bottomCenter,
child: ElevatedButton(
onPressed: enableFlash,
child: const Text("Flash"),
),
),
],
),
);
}
}
Upvotes: 0
Views: 17