Reputation: 21
Tried call back function to check the get the error , but app return to home screen without logging error. Given camera & media storage permission.
try {
var _picker = ImagePicker();
XFile? pickedFile = await _picker
.pickImage(
source: ImageSource.camera,
)
.then((value) {
try {
bloc.emit(ShowToastState(file: value));
} catch (e) {
bloc.emit(ShowToastState(msg: e.toString()));
}
}).onError((error, stackTrace) {
bloc.emit(
ShowToastState(msg: error.toString()));
});
if (pickedFile != null) {
bloc.add(ProfileScreenProfileChangeEvent(
pickedFile,
ImageSource.camera,
));
}
} on Exception catch (e) {
bloc.emit(ShowToastState(msg: e.toString()));
handleCameraPermissionError(context, e);
}
Upvotes: 2
Views: 1900
Reputation: 1746
The crash might occur if there are permission-related issues with the camera or storage permissions. Even though you mentioned that you have granted the necessary permissions, there could be some device-specific differences or restrictions that are causing the crash.
Certain devices or models might have specific hardware configurations or software implementations that can cause compatibility issues with certain packages or features. So make sure to test on as many devices as possible.
So this may be you can add a method like pickImageWithPermission
to ask user camera and storage permission before picking image. It is always a good practice to do so. And permission_handler is the perfect plugin for that. This is how it goes.
import 'package:permission_handler/permission_handler.dart';
import 'package:image_picker/image_picker.dart';
Future<void> pickImageWithPermission() async {
PermissionStatus cameraPermissionStatus = await Permission.camera.status;
PermissionStatus storagePermissionStatus = await Permission.storage.status;
if (cameraPermissionStatus.isGranted && storagePermissionStatus.isGranted) {
// Permissions are already granted, proceed to pick file
pickFile();
} else {
Map<Permission, PermissionStatus> permissionStatuses = await [
Permission.camera,
Permission.storage,
].request();
if (permissionStatuses[Permission.camera]!.isGranted &&
permissionStatuses[Permission.storage]!.isGranted) {
// Permissions granted, proceed to pick file
pickFile();
} else {
// Permissions denied, handle accordingly (show an error message, request again, or emit your bloc state.)
// ...
}
}
}
Future<void> pickFile() async {
var picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.camera);
if (pickedFile != null) {
// Handle the picked file, emit bloc state
// ...
} else {
// No file was captured, emit bloc error state, or handle as you want
}
}
Upvotes: 0