title: How to add flash toggle button to Flutter camera (flutter_camera_ml_vision)
body: <p>I'm developing an app that has a barcode reader inside it. I'm currently using the package flutter_camera_ml_vision.</p>
  
  <p>I'm trying to add a flash on/off floating button but i can't activate the flash while the camera is running, only before.
  error:</p>
  
  <pre><code>I/System.out(19394): Torch Failed : CAMERA_IN_USE (4): setTorchMode:1732: Torch for camera "0" is not available due to an existing camera user
  </code></pre>
  
  <p>Also when the camera pops up the flash is disabled.</p>
  
  <p>How can I achieve this? I'm testing on Android.</p>
  
  <p>Current code:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  import 'package:flutter/widgets.dart';
  import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart';
  import 'package:camera/camera.dart';
  import 'package:torch/torch.dart';
  
  class ScanPage extends StatefulWidget {
    @override
    _ScanPageState createState() =&gt; _ScanPageState();
  }
  
  class _ScanPageState extends State&lt;ScanPage&gt; {
    bool resultSent = false;
    bool _hasFlash = true;
    bool _isOn = false;
  
    @override
    void initState() {
      super.initState();
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
      initPlatformState();
    }
  
    @override
    dispose() {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      super.dispose();
    }
  
    initPlatformState() async {
      bool hasFlash = await Torch.hasTorch;
      print("Device has flash ? $hasFlash");
      setState(() { _hasFlash = hasFlash; });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            await _turnFlash();
          },
          child: Icon(_isOn ? Icons.flash_off : Icons.flash_on),
          backgroundColor: Colors.green,
        ),
        body: SafeArea(
          child: SizedBox(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: CameraMlVision&lt;List&lt;Barcode&gt;&gt;(
              resolution: ResolutionPreset.medium,
              detector: FirebaseVision.instance.barcodeDetector().detectInImage,
              onResult: (List&lt;Barcode&gt; barcodes) {
                if (!mounted || resultSent || barcodes.isEmpty) {
                  return;
                }
                resultSent = true;
                Navigator.of(context).pop&lt;Barcode&gt;(barcodes.first);
              },
            ),
          ),
        ),
      );
    }
  
    Future _turnFlash() async {
      _isOn ? Torch.turnOff() : Torch.turnOn();
      var f = await Torch.hasTorch;
      setState((){
        _hasFlash = f;
        _isOn = !_isOn;
      });
    }
  }
  
  </code></pre>