Clueless
Clueless

Reputation: 83

Flutter: Firebase Storage does not upload file on release mode

say I have a raisedbutton that has an onPressed: ()=> uploadFile(_image.path) // File _image = new File(); and here's my function.

Future<Null> uploadFile(String filepath) async {

final String fileName = '$myImage.jpg';
final ByteData bytes = await rootBundle.load(filepath);
final Directory tempDir = Directory.systemTemp;
final File file = File('${tempDir.path}/$fileName');
file.writeAsBytes(bytes.buffer.asInt8List(), mode: FileMode.write);
final StorageReference storageRef =
    FirebaseStorage.instance.ref().child('justAnImage/$fileName');
final StorageUploadTask task = storageRef.putFile(file);

final Uri downloadUrl = (await task.future).downloadUrl;
path = downloadUrl.toString();
return path;

}

It does upload an image to Firebase Storage on debug mode, but on release mode. it gives me an error. Cannot open file, path = '/data/local/tmp/myImage123.jpg' (OS Error: Permission denied, errno = 13)

I've already setup my Cloud Storage to allow read and write in security rules.

Upvotes: 1

Views: 714

Answers (1)

Clueless
Clueless

Reputation: 83

I fixed it with a package called path_provider and change the following code

Before:

final Directory tempDir = Directory.systemTemp;

After:

final Directory tempDir = await getTemporaryDirectory();

Upvotes: 3

Related Questions