Repository: flutter
Issue: [camera-web] [mobile] Switching cameras fails with `Uncaught PlatformException`
Labels: c: crash,platform-web,p: camera,package,has reproducible steps,P2,found in release: 3.10,found in release: 3.11,team-web,triaged-web

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

When using the described example, I am unable to change from face-camera to picture-camera in web. I native it works perfectly well.

```
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

import '../../../../common/globals.dart';
import '../../../message/message.dart';

class MWorkCamera extends StatefulWidget{
  const MWorkCamera({super.key});

  @override
  State<MWorkCamera> createState() => _MWorkCameraState();
}

class _MWorkCameraState extends State<MWorkCamera> {

  CameraController? _cameraController;
  late Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    if(currentCamera!=null) {
      _cameraController = CameraController(
        currentCamera!,
        ResolutionPreset.ultraHigh,
        //TODO: Resolution should be selectable
      );
    }

    if(_cameraController!=null) {
      _initializeControllerFuture = _cameraController!.initialize();
    }

  }

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
        future: _initializeControllerFuture,
        builder: (context, snapshot){
          if(snapshot.connectionState==ConnectionState.done){
            if(_cameraController!=null) {
              return Stack(
                children: [
                  SizedBox(
                      height: double.infinity,
                      width: double.infinity,
                      child: CameraPreview(_cameraController!)
                  ),
                  Positioned.fill(
                      child: Align(
                        alignment: Alignment.bottomCenter,
                        child: Padding(
                          padding: const EdgeInsets.only(bottom:4.0),
                          child: ElevatedButton(
                              onPressed: () async {
                                try {
                                  var pictureFile = await _cameraController
                                      ?.takePicture();
                                  Message.info("${pictureFile?.path.toString()}",durationSeconds: 60);
                                }catch(exception){
                                  Message.error(exception.toString());
                                }
                              },
                              child: const Icon(Icons.photo_camera),),
                        )
                      )
                  ),
                  Positioned.fill(
                      child: Align(
                        alignment: Alignment.bottomRight,
                        child: Padding(
                          padding: const EdgeInsets.all(4.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              OutlinedButton(
                                  onPressed: () async {


                                  },
                                  child: Icon(Icons.info_outline)
                              ),
                              OutlinedButton(
                                  onPressed: (cameras?.length??0)>=2?(){
                                    toggleCameraLens();
                                  }:null,
                                  child: Icon(Icons.cameraswitch_outlined)
                              )

                            ],
                          ),
                        ),
                      )
                  )

                ],
              );
            } else {
              return const Center(child: Card(child: Text("Camera not properly initialized"),),);
            }
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        }
      );
  }

  void toggleCameraLens(){
    print("cameras?.length = ${cameras?.length}");
    if((cameras?.length??0)<2) return;
    var lensDirection = _cameraController?.description.lensDirection;
    if(lensDirection == CameraLensDirection.front){
      print("cameras.first = ${cameras?.first}");
      setState(() {
        _cameraController = CameraController(
            cameras!.first,
            ResolutionPreset.ultraHigh);
      });
    } else {
      print("cameras.last = ${cameras?.last}");
      setState(() {
        _cameraController = CameraController(
            cameras!.last,
            ResolutionPreset.ultraHigh);
      });
    }

    if(_cameraController!=null) {
      _initializeControllerFuture = _cameraController!.initialize();
    }
  }


}
```

The `camras` variable is achieved in `main.dart`:

```
...
 cameras = await availableCameras();
 
     if(cameras?.isEmpty??true){
      Message.warn("Could not find camera, camera functions will be turned off !!");
    }

    currentCamera = cameras?.first;

    if(currentCamera==null){
      Message.error("Camera definition detected, but could not be read, camera functions are turned off !!");
    }
    
...

 
```

Tested in:
Samsung S9+ android 10: native ok, web fails.
Asus :native ok, web fails
Emulator Pixel 4 API 33: native ok, web fails.


### Expected results

Expected the camera to be shifted.

Both cameras are listed, shifts correctly in native,  but will not shift in web.



### Actual results

Please see expected results and steps to reproduce.

### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

import '../../../../common/globals.dart';
import '../../../message/message.dart';

class MWorkCamera extends StatefulWidget{
  const MWorkCamera({super.key});

  @override
  State<MWorkCamera> createState() => _MWorkCameraState();
}

class _MWorkCameraState extends State<MWorkCamera> {

  CameraController? _cameraController;
  late Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    if(currentCamera!=null) {
      _cameraController = CameraController(
        currentCamera!,
        ResolutionPreset.ultraHigh,
        //TODO: Resolution should be selectable
      );
    }

    if(_cameraController!=null) {
      _initializeControllerFuture = _cameraController!.initialize();
    }

  }

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
        future: _initializeControllerFuture,
        builder: (context, snapshot){
          if(snapshot.connectionState==ConnectionState.done){
            if(_cameraController!=null) {
              return Stack(
                children: [
                  SizedBox(
                      height: double.infinity,
                      width: double.infinity,
                      child: CameraPreview(_cameraController!)
                  ),
                  Positioned.fill(
                      child: Align(
                        alignment: Alignment.bottomCenter,
                        child: Padding(
                          padding: const EdgeInsets.only(bottom:4.0),
                          child: ElevatedButton(
                              onPressed: () async {
                                try {
                                  var pictureFile = await _cameraController
                                      ?.takePicture();
                                  Message.info("${pictureFile?.path.toString()}",durationSeconds: 60);
                                }catch(exception){
                                  Message.error(exception.toString());
                                }
                              },
                              child: const Icon(Icons.photo_camera),),
                        )
                      )
                  ),
                  Positioned.fill(
                      child: Align(
                        alignment: Alignment.bottomRight,
                        child: Padding(
                          padding: const EdgeInsets.all(4.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              OutlinedButton(
                                  onPressed: () async {


                                  },
                                  child: Icon(Icons.info_outline)
                              ),
                              OutlinedButton(
                                  onPressed: (cameras?.length??0)>=2?(){
                                    toggleCameraLens();
                                  }:null,
                                  child: Icon(Icons.cameraswitch_outlined)
                              )

                            ],
                          ),
                        ),
                      )
                  )

                ],
              );
            } else {
              return const Center(child: Card(child: Text("Camera not properly initialized"),),);
            }
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        }
      );
  }

  void toggleCameraLens(){
    print("cameras?.length = ${cameras?.length}");
    if((cameras?.length??0)<2) return;
    var lensDirection = _cameraController?.description.lensDirection;
    if(lensDirection == CameraLensDirection.front){
      print("cameras.first = ${cameras?.first}");
      setState(() {
        _cameraController = CameraController(
            cameras!.first,
            ResolutionPreset.ultraHigh);
      });
    } else {
      print("cameras.last = ${cameras?.last}");
      setState(() {
        _cameraController = CameraController(
            cameras!.last,
            ResolutionPreset.ultraHigh);
      });
    }

    if(_cameraController!=null) {
      _initializeControllerFuture = _cameraController!.initialize();
    }
  }


}
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

[native ok.webm](https://github.com/flutter/flutter/assets/11217296/e53795a0-0526-4e44-b080-dbb9f50fcb0e)

[web not ok.webm](https://github.com/flutter/flutter/assets/11217296/a3391100-6edf-4b63-9f37-076acb871023)

</details>


### Logs

<details><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
PS C:\FlutterProjects\mwork> flutter doctor -v
[√] Flutter (Channel beta, 3.11.0-0.1.pre, on Microsoft Windows [Version 10.0.22621.1702], locale nb-NO)
    • Flutter version 3.11.0-0.1.pre on channel beta at c:\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 350d2c3a6c (8 days ago), 2023-05-23 15:52:05 -0700
    • Engine revision 7eca5cd1c5
    • Dart version 3.1.0 (build 3.1.0-63.1.beta)
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at C:\Users\RoarGronmo\AppData\Local\Android\sdk
    • Platform android-UpsideDownCakePrivacySandbox, build-tools 33.0.2
    • Java binary at: C:\Program Files\Android Studio\Android Studio 2023.1.1.6\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.3)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31702.278
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2022.1)
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2022.1.1.21
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-9505619)

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2022.2.1.20
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] Android Studio (version 2022.2)
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2022.2.1.16
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] Android Studio (version 2022.3)
    • Android Studio at C:\Program Files\Android Studio\Android Studio 2022.3.1.15
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.22621.1702]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 113.0.5672.127
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 113.0.1774.57

[√] Network resources
    • All expected network resources are available.

• No issues found!
PS C:\FlutterProjects\mwork>

```

</details>