Skip to content

[camera] CameraController.setDescription and dispose don't work as expected #126823

@jellynoone

Description

@jellynoone

Is there an existing issue for this?

Steps to reproduce

  1. Initialize a CameraController
  2. Set a different CameraDescription via setDescription
  3. Dispose of the controller
  4. Initialize a new CameraController
  5. Observer a ChangeNotifier exception

Expected results

No ChangeNotifier exceptions.

Actual results

E/flutter (12815): Once you have called dispose() on a CameraController, it can no longer be used.
E/flutter (12815): #0      ChangeNotifier.debugAssertNotDisposed.<anonymous closure>
change_notifier.dart:179
E/flutter (12815): #1      ChangeNotifier.debugAssertNotDisposed
change_notifier.dart:186
E/flutter (12815): #2      ChangeNotifier.notifyListeners
change_notifier.dart:382
E/flutter (12815): #3      ValueNotifier.value=
change_notifier.dart:530
E/flutter (12815): #4      CameraController._initializeWithDescription.<anonymous closure>
camera_controller.dart:304
E/flutter (12815): #5      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (12815): #6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (12815): #7      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (12815): #8      _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:377:25)
E/flutter (12815): #9      _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:244:5)
E/flutter (12815): #10     Where.whereType.<anonymous closure>
where.dart:21
E/flutter (12815): #11     TransformByHandlers.transformByHandlers.<anonymous closure>.<anonymous closure>
from_handlers.dart:29
E/flutter (12815): #12     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (12815): #13     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (12815): #14     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
E/flutter (12815): #15     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
E/flutter (12815): #16     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
E/flutter (12815): #17     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (12815): #18     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Code sample

Not providing a code sample since, I think I know where the issue is:

If we look at:
CameraController.setDescription we see the new description is set up by calling _initializeWithDescription.

If we look at _initializeWithDescription, we see _deviceOrientationSubscription is set up through direct assignment and subscription to the CameraPlatform.instance.onDeviceOrientationChanged() stream.

Code:

      _deviceOrientationSubscription = CameraPlatform.instance
          .onDeviceOrientationChanged()
          .listen((DeviceOrientationChangedEvent event) {
        value = value.copyWith(
          deviceOrientation: event.orientation,
        );
      });

The _deviceOrientationSubscription is never cancelled in case we set up a new description, it is simply reassigned.

My guess would be to simply assign the _deviceOrientationSubscription using ??=.

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.0, on macOS 11.7.6 20G1231 darwin-x64, locale en-GB)
    • Flutter version 3.10.0 on channel stable at /usr/local/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 84a1e904f4 (6 days ago), 2023-05-09 07:41:44 -0700
    • Engine revision d44b5a94c9
    • Dart version 3.0.0
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/jellynoone/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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-17.0.6b802.4-9586694)

[✓] VS Code (version 1.79.0-insider)
    • VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
    • Flutter extension version 3.64.0

[✓] Connected device (3 available)
    • Pixel (mobile)  • FA76R0301797 • android-arm64  • Android 10 (API 29)
    • macOS (desktop) • macos        • darwin-x64     • macOS 11.7.6 20G1231 darwin-x64
    • Chrome (web)    • chrome       • web-javascript • Google Chrome 113.0.5672.92

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.10Found to occur in 3.10found in release: 3.11Found to occur in 3.11has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions