ImagePicker return invalid image source - flutter

Im using ImagePicker to pick multiple image. On android just work fine. But iOS returning.
PlatformException (PlatformException(invalid_source, Invalid image source.
ImagePicker _picker = ImagePicker();
pickMultiImage() async {
Size size = MediaQuery.of(context).size;
AppLocalizations localization = AppLocalizations.of(context)!;
try {
final List<XFile>? images = await _picker.pickMultiImage();
if (images!.isNotEmpty) {
imageFileList!.addAll(images);
}
setState(() {});
} catch (e) {
showDialogOpenPermission(
context, localization, localization.gallery, size);
}
}
update : Checking on iOS real device just works fine. Long ago, I think just works fine on emulator :(.
** I'm prefer to test my apps by via emulator. So, If anyone here know the solution, please tell me ;)

Related

How does flutter get the main color captured in the camera?

I'm building an app that uses a camera to capture pictures, and I need to prevent the lens from being blocked and display all black.
I'm using camera library.
Future<void> initController() async {
try {
List cameras = await availableCameras();
controller = CameraController(cameras.first, ResolutionPreset.low);
await controller!.initialize();
Future.delayed(const Duration(milliseconds: 100)).then((onValue) {
controller!.setFlashMode(FlashMode.torch);
});
controller!.startImageStream((CameraImage img) {
image = img;
});
} on Exception {
print(Exception);
}
}
I hope to use the CameraImage type that comes with the library to obtain the color code in some way in startImageStream to determine whether the user blocks the camera.

How to resolve Lost Connection to Device (Flutter Image Picker)

I'm building an app that requires me to use the camera of the device. I have followed all the procedures to set it up via the Image Picker Documentation and I am still having issues.
Funny enough, it was working fine when I tested it last time but as I added new widgets to screen and tried testing again, the app crashed with very minimal error message which says:
Lost connection to device.
This is how I capture the image:
Future<void> _pickImage() async {
try {
bool isPermisionGranted = await _requestFilePermission();
if (!isPermisionGranted) {
showToast('Please give us access to your camera.');
return;
}
final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 90);
if (image == null) return;
final tempImageFile = File(image.path);
setState(() {
_imageFile = tempImageFile;
});
if (!mounted) return;
Navigator.of(context).pushNamed(Routes.chi, arguments: {'image_file': _imageFile});
} on PlatformException catch (e) {
debugPrint('Failed to pick image: $e');
showToast('Failed to pick image');
}
}
Could this be a memory issue? And how can I resolve it?

Dart shared preferences not saving to disk

I've spent hours debugging this and can't find anything wrong with my code. I am on a MacOS, Big Sur, using VSCode and iOS emulator.
The shared preferences bloc returns that the storage was a success, but when I look on disk where the directory storage is, there is no file or anything there. This was just working the other day, but I did update flutter master. I downgraded to flutter beta but am having the same problem.
Does anyone see anything wrong with this or having a similar issue?
#override
Future<Either<Failure, UserEntity>> putStoredUserLoginModel() async {
final SharedPreferences sp = await SharedPreferences.getInstance();
final userMember = getUserMember();
await sp.setInt('foo', 1);
final userModel = userMember.toJson().toString();
print('PUTING: $userModel');
final bool notFailed = await sp.setString('userModel', userModel);
if (notFailed) {
return const Right(UserEmpty());
} else {
return Left(CacheFailure());
}
}

Flutter Web Image Picker and Uploading Image to Firebase

Flutter 2 announced just few days ago and some packages updated for web like Image Picker. I'm trying to get image and upload it to cloud storage. Image picker working very well but when
I want to upload image to storage it gives me error.
Unsupported operation: Platform._operatingSystem
UI Code
onImageSelect: (selectedImage) async {
try {
final url = await Get.find<FirebaseStorageService>()
.uploadProfilePicture(
customer.uuid, File(selectedImage.path));
print(url);
} catch (e) {
print(e);
}
customer.profileImageUrl = selectedImage.path;
_isImageSelected = true;
}),
Service Class funciton:
class FirebaseStorageService {
final FirebaseStorage _firebaseStorage = FirebaseStorage.instance;
Reference _storageReference;
Future<String> uploadProfilePicture(
String userID, File uploadingImagePath) async {
_storageReference = _firebaseStorage
.ref()
.child(userID)
.child('images')
.child("profile-photo.png");
var uploadTask = _storageReference.putFile(uploadingImagePath);
var url = await (await uploadTask).ref.getDownloadURL();
return url;
}
}
I tried flutter clean and flutter upgrade. I also searched old questions and some of them used html.File instead of dart:io library but firebase_storage:7.0.0 package only accept File class which is from dart:io library.
Code work on Android and IOS but not web.
I'm using stable channel.

White/blank screen after close camera in Flutter

I have a widget to take pictures on my app working like so:
final File imageFile =
await ImagePicker.pickImage(source: ImageSource.camera).then((test) {
print('TEST $test');
return test;
});
I can open the camera without any error and also take the picture but when I try to go back or accept the picture I've taken the app displays an white screen and the console show no errors at all.
This is failing on a real device (Xiaomi Redmi Note 8t) but it works on Android Emulator.
The only message I can see is Lost connection to device. when I take the camera
Fixed adding a try catch:
Future<Null> _pickImageFromCamera(BuildContext context, int index) async {
File imageFile;
try {
imageFile = await ImagePicker.pickImage(source: ImageSource.camera)
.then((picture) {
return picture; // I found this .then necessary
});
} catch (eror) {
print('error taking picture ${error.toString()}');
}
setState(() => this._imageFile = imageFile);
}
I found the solution for flutter 2
In Android
You need to add this to your 3 manifest (debug, main , profile)
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Then pick the image with a try catch, like this
Future getbehind() async {
try{
final pickedFile = await picker.getImage(source: (ImageSource.camera:ImageSource.gallery),)
.then((value) {
setState(() {
if (value != null) {
behind = File(value.path);
} else {
print('No image selected.');
}
});
});
}catch(e){
}
Run the app in a release mode and its done!