Search code examples
flutterfirebasefirebase-storage

Failed assertion: line 127 pos 12: 'file.absolute.existsSync()': is not true.)


When i run my App anythings is ok, but if want to register a new user, i always get this exception:

Failed assertion: line 127 pos 12: 'file.absolute.existsSync()': is not true.)

I use Firebase Storage

enter image description here

I declare a variables

XFile? imageXfile;
final ImagePicker _picker = ImagePicker();

Here i get image to galerie

  Future<void> _getImage() async {
    imageXfile = await _picker.pickImage(source: ImageSource.gallery);
    setState(() {
      imageXfile;
    });
  }

Here a save a new register with images

//upload image to  Firebase Storage    
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
Reference storageReference =
        FirebaseStorage.instance.ref().child('images/$fileName');
UploadTask uploadTask =
    storageReference.putFile(File(imageXfile!.path));
TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() {});
sallerImage = await taskSnapshot.ref.getDownloadURL();

await FirebaseFirestore.instance
    .collection('images')
    .add({'url': sallerImage});

//save iamge 
SingUpAndAutheticated();

Solution

  • File(imageXfile!.path)
    

    Make sure that the picking operation ended with a picked file, which leads to an initialized imageXFile not null.

    you need to check the nullability of imageXFile before uploading it, because if it's null an Exception that indicates that file is not exist will be thrown.

     if(imageXFile != null)
          {
            setState(() {
              // upload it 
              // do whatever you want
            });
          }