fix: improve camera controller state management by ensuring proper initialization and disposal, enhancing image capture reliability
This commit is contained in:
@@ -426,3 +426,4 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -20,17 +20,21 @@ class RImagePickerController extends ChangeNotifier {
|
||||
try {
|
||||
isLoading = true;
|
||||
await disposeCameraController();
|
||||
|
||||
await getAvailableCameras();
|
||||
|
||||
if (_cameras.isNotEmpty) {
|
||||
cameraController = CameraController(
|
||||
_cameras[0],
|
||||
ResolutionPreset.high,
|
||||
enableAudio: false,
|
||||
);
|
||||
|
||||
await cameraController?.initialize();
|
||||
notifyListeners();
|
||||
|
||||
isCameraReady = true;
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
} else {
|
||||
isCameraReady = false;
|
||||
isLoading = false;
|
||||
@@ -45,8 +49,9 @@ class RImagePickerController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> takePicture() async {
|
||||
if (cameraController == null || !cameraController!.value.isInitialized)
|
||||
if (cameraController == null || !cameraController!.value.isInitialized) {
|
||||
return;
|
||||
}
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
@@ -73,6 +78,9 @@ class RImagePickerController extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> disposeCameraController() async {
|
||||
isCameraReady = false;
|
||||
notifyListeners();
|
||||
await cameraController?.dispose();
|
||||
cameraController = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user