Reputation: 987
In my below code future is not working ...edited code is below ###########################################################################################################################################################################################################################################################################################################################################################
void compressImage() async {
print('startin');
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
int rand = new Math.Random().nextInt(99999999);
Im.Image image = Im.decodeImage(file.readAsBytesSync());
Im.copyResize(image, 500);
// image.format = Im.Image.RGBA;
// Im.Image newim = Im.remapColors(image, alpha: Im.LUMINANCE);
var newim2 = new File('$path/img_$rand.jpg')
..writeAsBytesSync(Im.encodeJpg(image, quality: 85));
setState(() {
file = newim2;
});
print('done');
}
void clearImage() {
setState(() {
file = null;
});
}
void postImage() {
setState(() {
uploading = true;
});
compressImage();
Future<String> upload = uploadImage(file).then((String data) {
Firestore.instance.collection("Advertice").document().setData({
"Content": discription,
"title": title.toUpperCase(),
"url": data,
});
}).then((_) {
setState(() {
file = null;
uploading = false;
});
});
}
}
Future<String> uploadImage(var imageFile) async {
var uuid = new Uuid().v1();
StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
StorageUploadTask uploadTask = ref.put(imageFile);
Uri URL = (await uploadTask.future).downloadUrl;
return URL.toString();
}
Upvotes: 1
Views: 5107
Reputation: 1
This should work :
Future<String> uploadImage(var imageFile) async {
var uuid = new Uuid().v1();
StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
StorageTaskSnapshot snapshot = await ref.putFile(image).onComplete;
return await snapshot.ref.getDownloadURL();
}
Upvotes: 0
Reputation: 51226
Try this: You will get the File Download URL:
String downloadUrl;
Future<String> uploadImage(var imageFile) async {
var uuid = new Uuid().v1();
StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
await ref.put(imageFile).onComplete.then((val) {
val.ref.getDownloadURL().then((val) {
print(val);
downloadUrl = val; //Val here is Already String
});
});
return downloadUrl;
}
Upvotes: 3
Reputation: 18640
You could do this and get the download url from the return value of this method.
Future<UploadTaskSnapshot> uploadImage(var imageFile) {
var uuid = new Uuid().v1();
StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
StorageUploadTask uploadTask = ref.put(imageFile);
// Uri downloadUrl = (await uploadTask.future).downloadUrl;
return uploadTask.future;
}
You can get the download url like this anywhere within the class:
String downloadUrl = uploadImage(imageFile).downloadUrl.toString();
Upvotes: -1