'file.existsSync()': is not true

Viewed 4652

I am going to store an image in firebase storage. When I send file through image picker then its working fine. But when I manually pass link of image then it is showing an error that says:

'package:firebase_storage/src/storage_reference.dart': 
Failed assertion: line 62 pos 12: 'file.existsSync()': is not true.

I am writing following code:

File image =  File("assets/img/pic.jpg");

final StorageReference firebaseStorageRef =
        FirebaseStorage.instance.ref().child('image');

InkWell(
            child: Text("Tap to Upload"),
            onTap: () {
              firebaseStorageRef.putFile(image);
            },
          ),
1 Answers

The error says it, the file doesn't exists, before doing that you should check if the file is there or create it if it's not

InkWell(
   child: Text("Tap to Upload"),
   onTap: () async {
     File image = await File("assets/img/pic.jpg").create(); 
     // it creates the file,
     // if it already existed then just return it
     // or run this if the file is created before the onTap
     // if(image.existsSync()) image = await image.create();
     firebaseStorageRef.putFile(image);
   },
),

Also I'm not sure if you can change/create files in the assets folder, if that doesn't work maybe try to put the file in a temp directory of your app

Related