0

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

3
  • 1
    Remove the "". _imageFile=File(photo!.path);. Try this Commented Feb 9, 2022 at 17:25
  • its not resolve my problem Commented Feb 9, 2022 at 17:37
  • After removing "", getting the same error? It's not possible unless you are using the same variable name (_imageFile ) in somewhere else which has the type of List<Object>. Maybe checking other codes helps. Commented Feb 10, 2022 at 2:03

3 Answers 3

1

To pick image

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

And use like

if(pickedFile !=null) Image.file(File(pickedFile.path)),
Sign up to request clarification or add additional context in comments.

6 Comments

again the same error
Can you check the imagePicker version
image_picker: ^0.8.4+7
Can you restart your IDE after having Image.file(File(pickedFile.path))
idk i do all what u suggestion and no changes
|
1
///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;
      }

Comments

0

try use if to make a function return File

if (picedfile!= null) {
  return File(picedfile.path);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.