Home > database >  imagePicker Flutter can't convert image to FIle
imagePicker Flutter can't convert image to FIle

Time:02-10

i wanna convert my photo from gallery to File i need that beacause i will take the image to firebase when i try convert to File, it will throw me error The argument type 'String' can't be assigned to the parameter type 'List'

My code:

File? _imageFile;
final picker = ImagePicker();

Future  getImage() async{
 final  photo = await _picker.pickImage(source: ImageSource.gallery);  
_imageFile=File(photo!.path,""); #that line give error 
 }

enter image description here

EDIT: I DELETE "import 'dart:html';" and it work

CodePudding user response:

To pick image

 final XFile? pickedFile = await ImagePicker().pickImage(
      source: ImageSource.gallery,
    );

And use like

if(pickedFile !=null) Image.file(File(pickedFile.path)),

CodePudding user response:

///I used this code for image picking and uploading the image to firebase          
  File? _image;
            final picker = ImagePicker();
            ///pop-up to choose camera gallery while uploading image
              Future<void> _showChoiceDialog(BuildContext context) {
                return showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text(
                          "Choose option",
                          style: TextStyle(color: AppColors.hotelListDarkBlue),
                        ),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: [
                              Divider(
                                height: 1,
                                color: AppColors.baseLightBlueColor,
                              ),
                              ListTile(
                                onTap: () {
                                  Navigator.pop(context, _pickImage(ImageSource.gallery,context));
                                },
                                title: Text(
                                  "Gallery",
                                  style: TextStyle(color: AppColors.hotelListDarkBlue),
                                ),
                                leading: Icon(
                                  Icons.account_box,
                                  color: AppColors.baseLightBlueColor,
                                ),
                              ),
                              Divider(
                                height: 1,
                                color: AppColors.baseLightBlueColor,
                              ),
                              ListTile(
                                onTap: () {
                                  Navigator.pop(context, _pickImage(ImageSource.camera,context));
                                },
                                title: Text(
                                  "Camera",
                                  style: TextStyle(color: AppColors.hotelListDarkBlue,),
                                ),
                                leading: Icon(
                                  Icons.camera,
                                  color: AppColors.baseLightBlueColor,
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    });
              }
            
            ///ImageSource: Camera and Gallery.
              Future<Null> _pickImage(ImageSource source,context) async {
                final pickedImage =
                await ImagePicker().getImage(source: source);
                _image = pickedImage != null ? File(pickedImage.path) : null;
                if (_image != null) {
        //you can set your value
                  setState(() {
                    //state = AppState.picked;
            
            
                  });
                
                }
              }
    
    ///I use CachedNetworkImage o show image I used provider you can skip the step and set your image in imageUrl
            
            CachedNetworkImage(
                                              maxHeightDiskCache: 100,
                                              imageUrl: provider
                                                  .currentLoggedInUser!.profilePicture
                                                  .toString(),
                                              imageBuilder: (context, imageProvider) =>
                                                  Container(
                                                height: 120,
                                                width: 120,
                                                decoration: BoxDecoration(
                                                  shape: BoxShape.circle,
                                                  image: DecorationImage(
                                                      image: imageProvider,
                                                      fit: BoxFit.cover),
                                                  border: Border.all(
                                                      color: AppColors.white,
                                                      width: 2.0),
                                                ),
                                              ),
                                              placeholder: (context, url) =>
                                                  const CircularProgressIndicator(),
                                              errorWidget: (context, url, error) =>
                                                  Container(
                                                height: 120,
                                                width: 120,
                                                decoration: BoxDecoration(
                                                  color: Colors.white,
                                                  shape: BoxShape.circle,
                                                  image: DecorationImage(
                                                      fit: BoxFit.cover,
                                                      image: AssetImage(
                                                          'assets/managerPicture.jpeg')),
                                                  border: Border.all(
                                                      color: AppColors.white,
                                                      width: 2.0),
                                                ),
                                              ),
                                              fadeOutDuration:
                                                  const Duration(seconds: 1),
                                              fadeInDuration:
                                                  const Duration(seconds: 3),
                                            ),
    
    ///to upload image i used thise method
    String ?url;
      Future <String?>uploadImageToFirebase(File _image) async {
        User? currentUser = FirebaseAuth.instance.currentUser;
        String fileName = basename(_image.path);
        firebase_storage.Reference ref = firebase_storage.FirebaseStorage.instance
            .ref().child('uploads')
            .child('/$fileName');
        final metadata = firebase_storage.SettableMetadata(
            contentType: 'image/jpeg',
            customMetadata: {'picked-file-path': fileName});
        firebase_storage.UploadTask uploadTask;
        uploadTask = ref.putFile(io.File(_image.path), metadata);
        //String ?url;
        await uploadTask.whenComplete(() async {
          url = await uploadTask.snapshot.ref.getDownloadURL();
        });
        Future.value(uploadTask)
            .then((value) => {
          print("Upload file path ${value.ref.fullPath}"),
          FirebaseFirestore.instance.collection(FirestoreCollections.employees).doc(currentUser!.uid).update({'image': '$url'}),
        })
            .onError((error, stackTrace) =>
        {
          print("Upload file path error ${error.toString()} ")}
          );
        return url;
      }
  • Related